Smarthome Functionen
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

helpers.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from base import common_base
  5. import config
  6. import geo
  7. import task
  8. import time
  9. def now():
  10. return time.mktime(time.localtime())
  11. def next_sunrise_time(time_offs_min=30):
  12. tm = now()
  13. rv = time.mktime(geo.sun.sunrise(config.GEO_POSITION)) + time_offs_min * 60
  14. if tm > rv:
  15. rv = time.mktime(geo.sun.sunrise(config.GEO_POSITION, date=time.localtime(tm + 24 * 60 * 60))) + time_offs_min * 60
  16. return rv
  17. def next_sunset_time(time_offs_min=-30):
  18. tm = now()
  19. rv = time.mktime(geo.sun.sunset(config.GEO_POSITION)) + time_offs_min * 60
  20. if tm > rv:
  21. rv = time.mktime(geo.sun.sunset(config.GEO_POSITION, date=time.localtime(tm + 24 * 60 * 60))) + time_offs_min * 60
  22. return rv
  23. def next_user_time(hh, mm):
  24. ts = time.localtime()
  25. tm = time.mktime(ts)
  26. ut_ts = list(ts)
  27. ut_ts[3] = hh
  28. ut_ts[4] = mm
  29. ut = time.mktime(time.struct_time(list(ts[:3]) + [hh, mm, 0] + list(ts[6:])))
  30. if ts[3] > hh or (ts[3] == hh and ts[4] >= mm):
  31. ut += 24 * 60 * 60
  32. #
  33. return ut
  34. class day_state(common_base):
  35. """
  36. Class to subscribe day events as a callback (see add_callback)
  37. :param time_start_of_day: Time of a day (tuple including hour and minute) for start of day or None for no start of day state.
  38. :type time_start_of_day: tuple
  39. :param time_start_of_night: Time of a day (tuple including hour and minute) for start of night or None for no end of day state.
  40. :type time_start_of_night: tuple
  41. :param time_offset_sunrise: time offset for sunrise in minutes (negative values lead to earlier sunrise state) or None for no sunrise state.
  42. :type time_start_of_day: int
  43. :param time_offset_sunset: time offset for sunset in minutes (negative values lead to earlier sunset state) or None for no sunrise state.
  44. :type time_start_of_day: int
  45. """
  46. KEY_SUNRISE = 'sunrise'
  47. KEY_SUNSET = 'sunset'
  48. KEY_START_OF_NIGHT = 'start_of_night'
  49. KEY_START_OF_DAY = 'start_of_day'
  50. #
  51. STATES = (KEY_START_OF_DAY, KEY_SUNRISE, KEY_SUNSET, KEY_START_OF_NIGHT)
  52. def __init__(self, time_start_of_day, time_start_of_night, time_offset_sunrise, time_offset_sunset):
  53. self.__time_start_of_day__ = time_start_of_day
  54. self.__time_start_of_night__ = time_start_of_night
  55. self.__time_offset_sunrise__ = time_offset_sunrise
  56. self.__time_offset_sunset__ = time_offset_sunset
  57. super().__init__()
  58. #
  59. def get_state(self):
  60. tm = {}
  61. if self.__time_offset_sunrise__ is not None:
  62. tm[next_sunrise_time(self.__time_offset_sunrise__)] = self.KEY_SUNRISE
  63. if self.__time_start_of_day__ is not None:
  64. tm[next_user_time(*(self.__time_start_of_day__))] = self.KEY_START_OF_DAY
  65. if self.__time_offset_sunset__ is not None:
  66. tm[next_sunset_time(self.__time_offset_sunset__)] = self.KEY_SUNSET
  67. if self.__time_start_of_night__ is not None:
  68. tm[next_user_time(*(self.__time_start_of_night__))] = self.KEY_START_OF_NIGHT
  69. #
  70. tms = list(tm.keys())
  71. tms.sort()
  72. return tm[tms[-1]]
  73. class day_event(day_state):
  74. """
  75. Class to subscribe day events as a callback (see add_callback)
  76. :param time_start_of_day: Time of a day (tuple including hour and minute) for start of day or None for no start of day state.
  77. :type time_start_of_day: tuple
  78. :param time_start_of_night: Time of a day (tuple including hour and minute) for start of night or None for no end of day state.
  79. :type time_start_of_night: tuple
  80. :param time_offset_sunrise: time offset for sunrise in seconds (negative values lead to earlier sunrise state) or None for no sunrise state.
  81. :type time_start_of_day: int
  82. :param time_offset_sunset: time offset for sunset in seconds (negative values lead to earlier sunset state) or None for no sunrise state.
  83. :type time_start_of_day: int
  84. """
  85. def __init__(self, time_start_of_day=(6, 0), time_start_of_night=(22, 0), time_offset_sunrise=30, time_offset_sunset=-30):
  86. super().__init__(time_start_of_day, time_start_of_night, time_offset_sunrise, time_offset_sunset)
  87. #
  88. current_day_state = self.get_state()
  89. for key in self.STATES:
  90. self[key] = current_day_state == key
  91. #
  92. cyclic = task.periodic(30, self.__cyclic__)
  93. cyclic.run()
  94. def __cyclic__(self, a):
  95. current_day_state = self.get_state()
  96. for key in self.STATES:
  97. self.set(key, current_day_state == key)