您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

147 行
3.5KB

  1. from modules import cbpi
  2. from modules.core.props import StepProperty, Property
  3. import time
  4. class NotificationAPI(object):
  5. def notify(self, headline, message, type="success", timeout=5000):
  6. self.api.notify(headline, message, type, timeout)
  7. class ActorAPI(NotificationAPI):
  8. @cbpi.try_catch(None)
  9. def actor_on(self, id, power=100):
  10. self.api.switch_actor_on(int(id), power=power)
  11. @cbpi.try_catch(None)
  12. def actor_off(self, id):
  13. self.api.switch_actor_off(int(id))
  14. @cbpi.try_catch(None)
  15. def actor_power(self, id, power):
  16. self.api.actor_power(int(id), power)
  17. class SensorAPI(NotificationAPI):
  18. @cbpi.try_catch(None)
  19. def get_sensor_value(self, id):
  20. return cbpi.get_sensor_value(id)
  21. class KettleAPI(NotificationAPI):
  22. @cbpi.try_catch(None)
  23. def get_kettle_temp(self, id=None):
  24. id = int(id)
  25. if id is None:
  26. id = self.kettle_id
  27. return cbpi.get_sensor_value(int(self.api.cache.get("kettle").get(id).sensor))
  28. @cbpi.try_catch(None)
  29. def get_target_temp(self, id=None):
  30. id = int(id)
  31. if id is None:
  32. id = self.kettle_id
  33. return self.api.cache.get("kettle").get(id).target_temp
  34. def set_target_temp(self, temp, id=None):
  35. temp = float(temp)
  36. try:
  37. if id is None:
  38. self.api.emit_event("SET_TARGET_TEMP", id=self.kettle_id, temp=temp)
  39. else:
  40. self.api.emit_event("SET_TARGET_TEMP", id=id, temp=temp)
  41. except Exception as e:
  42. self.notify("Faild to set Target Temp", "", type="warning")
  43. class Timer(object):
  44. timer_end = Property.Number("TIMER_END", configurable=False)
  45. def start_timer(self, timer):
  46. if self.timer_end is not None:
  47. return
  48. self.timer_end = int(time.time()) + timer
  49. def stop_timer(self):
  50. if self.timer_end is not None:
  51. self.timer_end = None
  52. def is_timer_running(self):
  53. if self.timer_end is not None:
  54. return True
  55. else:
  56. return False
  57. def timer_remaining(self):
  58. if self.timer_end is not None:
  59. return self.timer_end - int(time.time())
  60. else:
  61. return None
  62. def is_timer_finished(self):
  63. if self.timer_end is None:
  64. return None
  65. if self.timer_end <= int(time.time()):
  66. return True
  67. else:
  68. return False
  69. class StepBase(Timer, ActorAPI, SensorAPI, KettleAPI):
  70. __dirty = False
  71. managed_fields = []
  72. n = False
  73. def next(self):
  74. self.n = True
  75. def init(self):
  76. pass
  77. def finish(self):
  78. pass
  79. def reset(self):
  80. pass
  81. def execute(self):
  82. print("-------------")
  83. print("Step Info")
  84. print("Kettle ID: %s" % self.kettle_id)
  85. print("ID: %s" % self.id)
  86. def __init__(self, *args, **kwds):
  87. for a in kwds:
  88. super(StepBase, self).__setattr__(a, kwds.get(a))
  89. self.api = kwds.get("api")
  90. self.id = kwds.get("id")
  91. self.name = kwds.get("name")
  92. self.kettle_id = kwds.get("kettleid")
  93. self.value = None
  94. self.__dirty = False
  95. def is_dirty(self):
  96. return self.__dirty
  97. def reset_dirty(self):
  98. self.__dirty = False
  99. def __setattr__(self, name, value):
  100. if name != "_StepBase__dirty" and name in self.managed_fields:
  101. self.__dirty = True
  102. super(StepBase, self).__setattr__(name, value)
  103. else:
  104. super(StepBase, self).__setattr__(name, value)