Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. devices (DEVICES)
  6. =================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports smarthome devices
  11. **Submodules:**
  12. * :mod:`shelly`
  13. * :mod:`silvercrest_powerplug`
  14. **Unittest:**
  15. See also the :download:`unittest <devices/_testresults_/unittest.pdf>` documentation.
  16. **Module Documentation:**
  17. """
  18. __DEPENDENCIES__ = []
  19. import json
  20. import logging
  21. try:
  22. from config import APP_NAME as ROOT_LOGGER_NAME
  23. except ImportError:
  24. ROOT_LOGGER_NAME = 'root'
  25. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  26. BATTERY_WARN_LEVEL = 5
  27. def is_json(data):
  28. try:
  29. json.loads(data)
  30. except json.decoder.JSONDecodeError:
  31. return False
  32. else:
  33. return True
  34. class base(dict):
  35. TX_TOPIC = "set"
  36. TX_VALUE = 0
  37. TX_DICT = 1
  38. TX_TYPE = -1
  39. TX_FILTER_DATA_KEYS = []
  40. #
  41. RX_KEYS = []
  42. RX_IGNORE_TOPICS = []
  43. RX_IGNORE_KEYS = []
  44. RX_FILTER_DATA_KEYS = []
  45. def __init__(self, mqtt_client, topic):
  46. # data storage
  47. self.mqtt_client = mqtt_client
  48. self.topic = topic
  49. # initialisations
  50. dict.__init__(self)
  51. mqtt_client.add_callback(
  52. topic=self.topic, callback=self.receive_callback)
  53. mqtt_client.add_callback(
  54. topic=self.topic+"/#", callback=self.receive_callback)
  55. #
  56. self.callback_list = []
  57. self.warning_callback = None
  58. #
  59. self.__previous__ = {}
  60. def receive_callback(self, client, userdata, message):
  61. self.unpack(message)
  62. def unpack_filter(self, key):
  63. if key in self.RX_FILTER_DATA_KEYS:
  64. if self.get(key) == 1 or self.get(key) == 'on' or self.get(key) == 'ON':
  65. self[key] = True
  66. elif self.get(key) == 0 or self.get(key) == 'off' or self.get(key) == 'OFF':
  67. self[key] = False
  68. def unpack_single_value(self, key, data):
  69. prev_value = self.get(key)
  70. if key in self.RX_KEYS:
  71. self[key] = data
  72. self.__previous__[key] = prev_value
  73. # Filter, if needed
  74. self.unpack_filter(key)
  75. logger.debug("Received data for (%s) %s - %s", self.topic, key, str(self.get(key)))
  76. self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
  77. elif key not in self.RX_IGNORE_KEYS:
  78. logger.warning('Got a message from \"%s\" with unparsed content "%s"', self.topic, key)
  79. else:
  80. logger.debug("Ignoring key %s", key)
  81. def unpack(self, message):
  82. content_key = message.topic[len(self.topic) + 1:]
  83. if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
  84. logger.debug("Unpacking content_key \"%s\" from message.", content_key)
  85. if is_json(message.payload):
  86. data = json.loads(message.payload)
  87. if type(data) is dict:
  88. for key in data:
  89. self.unpack_single_value(key, data[key])
  90. else:
  91. self.unpack_single_value(content_key, data)
  92. # String
  93. else:
  94. self.unpack_single_value(
  95. content_key, message.payload.decode('utf-8'))
  96. self.warning_caller()
  97. else:
  98. logger.debug("Ignoring topic %s", content_key)
  99. def pack_filter(self, key, data):
  100. if key in self.TX_FILTER_DATA_KEYS:
  101. if data is True:
  102. return "on"
  103. elif data is False:
  104. return "off"
  105. else:
  106. return data
  107. return data
  108. def set(self, key, data):
  109. self.pack(key, data)
  110. def pack(self, key, data):
  111. data = self.pack_filter(key, data)
  112. if self.TX_TOPIC is not None:
  113. if self.TX_TYPE < 0:
  114. logger.error(
  115. "Unknown tx type. Set TX_TYPE of class to a known value")
  116. else:
  117. logger.debug("Sending data for (%s) %s - %s", self.topic, key, str(data))
  118. if self.TX_TYPE == self.TX_DICT:
  119. self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
  120. else:
  121. if type(data) not in [str, bytes]:
  122. data = json.dumps(data)
  123. self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
  124. else:
  125. logger.error(
  126. "Unknown tx toptic. Set TX_TOPIC of class to a known value")
  127. def add_callback(self, key, data, callback, on_change_only=False):
  128. """
  129. key: key or None for all keys
  130. data: data or None for all data
  131. """
  132. cb_tup = (key, data, callback, on_change_only)
  133. if cb_tup not in self.callback_list:
  134. self.callback_list.append(cb_tup)
  135. def add_warning_callback(self, callback):
  136. self.warning_callback = callback
  137. def warning_call_condition(self):
  138. return False
  139. def callback_caller(self, key, data, value_changed):
  140. for cb_key, cb_data, callback, on_change_only in self.callback_list:
  141. if (cb_key == key or cb_key is None) and (cb_data == data or cb_data is None) and callback is not None:
  142. if not on_change_only or value_changed:
  143. callback(self, key, data)
  144. def warning_caller(self):
  145. if self.warning_call_condition():
  146. warn_txt = self.warning_text()
  147. logger.warning(warn_txt)
  148. if self.warning_callback is not None:
  149. self.warning_callback(self, warn_txt)
  150. def warning_text(self, data):
  151. return "default warning text - replace parent warning_text function"
  152. def previous_value(self, key):
  153. return self.__previous__.get(key)
  154. class shelly(base):
  155. KEY_OUTPUT_0 = "relay/0"
  156. KEY_OUTPUT_1 = "relay/1"
  157. KEY_INPUT_0 = "input/0"
  158. KEY_INPUT_1 = "input/1"
  159. KEY_LONGPUSH_0 = "longpush/0"
  160. KEY_LONGPUSH_1 = "longpush/1"
  161. KEY_TEMPERATURE = "temperature"
  162. KEY_OVERTEMPERATURE = "overtemperature"
  163. KEY_ID = "id"
  164. KEY_MODEL = "model"
  165. KEY_MAC = "mac"
  166. KEY_IP = "ip"
  167. KEY_NEW_FIRMWARE = "new_fw"
  168. KEY_FIRMWARE_VERSION = "fw_ver"
  169. #
  170. TX_TOPIC = "command"
  171. TX_TYPE = base.TX_VALUE
  172. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1]
  173. #
  174. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OVERTEMPERATURE, KEY_TEMPERATURE,
  175. KEY_ID, KEY_MODEL, KEY_MAC, KEY_IP, KEY_NEW_FIRMWARE, KEY_FIRMWARE_VERSION]
  176. RX_IGNORE_TOPICS = [KEY_OUTPUT_0 + '/' + "energy", KEY_OUTPUT_1 + '/' + "energy", 'input_event/0', 'input_event/1']
  177. RX_IGNORE_KEYS = ['temperature_f']
  178. RX_FILTER_DATA_KEYS = [KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OVERTEMPERATURE]
  179. def __init__(self, mqtt_client, topic):
  180. super().__init__(mqtt_client, topic)
  181. #
  182. # WARNING CALL
  183. #
  184. def warning_call_condition(self):
  185. return self.get(self.KEY_OVERTEMPERATURE)
  186. def warning_text(self):
  187. if self.overtemperature:
  188. if self.temperature is not None:
  189. return "Overtemperature detected for %s. Temperature was %.1f°C." % (self.topic, self.temperature)
  190. else:
  191. return "Overtemperature detected for %s." % self.topic
  192. #
  193. # RX
  194. #
  195. @property
  196. def output_0(self):
  197. """rv: [True, False]"""
  198. return self.get(self.KEY_OUTPUT_0)
  199. @property
  200. def output_1(self):
  201. """rv: [True, False]"""
  202. return self.get(self.KEY_OUTPUT_1)
  203. @property
  204. def input_0(self):
  205. """rv: [True, False]"""
  206. return self.get(self.KEY_INPUT_0)
  207. @property
  208. def input_1(self):
  209. """rv: [True, False]"""
  210. return self.get(self.KEY_INPUT_1)
  211. @property
  212. def longpush_0(self):
  213. """rv: [True, False]"""
  214. return self.get(self.KEY_LONGPUSH_0)
  215. @property
  216. def longpush_1(self):
  217. """rv: [True, False]"""
  218. return self.get(self.KEY_LONGPUSH_1)
  219. @property
  220. def temperature(self):
  221. """rv: numeric value"""
  222. return self.get(self.KEY_TEMPERATURE)
  223. #
  224. # TX
  225. #
  226. def set_output_0(self, state):
  227. """state: [True, False, 'toggle']"""
  228. self.pack(self.KEY_OUTPUT_0, state)
  229. def set_output_0_mcb(self, device, key, data):
  230. logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
  231. self.set_output_0(data)
  232. def toggle_output_0_mcb(self, device, key, data):
  233. logger.info("%s: Toggeling output 0", self.topic)
  234. self.set_output_0('toggle')
  235. def set_output_1(self, state):
  236. """state: [True, False, 'toggle']"""
  237. self.pack(self.KEY_OUTPUT_1, state)
  238. def set_output_1_mcb(self, device, key, data):
  239. logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
  240. self.set_output_1(data)
  241. def toggle_output_1_mcb(self, device, key, data):
  242. logger.info("%s: Toggeling output 1", self.topic)
  243. self.set_output_1('toggle')
  244. class silvercrest_powerplug(base):
  245. KEY_LINKQUALITY = "linkquality"
  246. KEY_OUTPUT_0 = "state"
  247. #
  248. TX_TYPE = base.TX_DICT
  249. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  250. #
  251. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0]
  252. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  253. def __init__(self, mqtt_client, topic):
  254. super().__init__(mqtt_client, topic)
  255. #
  256. # RX
  257. #
  258. @property
  259. def output_0(self):
  260. """rv: [True, False]"""
  261. return self.get(self.KEY_OUTPUT_0)
  262. @property
  263. def linkquality(self):
  264. """rv: numeric value"""
  265. return self.get(self.KEY_LINKQUALITY)
  266. #
  267. # TX
  268. #
  269. def set_output_0(self, state):
  270. """state: [True, False, 'toggle']"""
  271. self.pack(self.KEY_OUTPUT_0, state)
  272. def set_output_0_mcb(self, device, key, data):
  273. logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
  274. self.set_output_0(data)
  275. def toggle_output_0_mcb(self, device, key, data):
  276. logger.info("%s: Toggeling output 0", self.topic)
  277. self.set_output_0('toggle')
  278. class my_powerplug(base):
  279. KEY_OUTPUT_0 = "output/1"
  280. KEY_OUTPUT_1 = "output/2"
  281. KEY_OUTPUT_2 = "output/3"
  282. KEY_OUTPUT_3 = "output/4"
  283. KEY_OUTPUT_ALL = "output/all"
  284. KEY_OUTPUT_LIST = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  285. #
  286. TX_TYPE = base.TX_VALUE
  287. #
  288. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  289. def __init__(self, mqtt_client, topic):
  290. super().__init__(mqtt_client, topic)
  291. #
  292. # RX
  293. #
  294. @property
  295. def output_0(self):
  296. """rv: [True, False]"""
  297. return self.get(self.KEY_OUTPUT_0)
  298. @property
  299. def output_1(self):
  300. """rv: [True, False]"""
  301. return self.get(self.KEY_OUTPUT_1)
  302. @property
  303. def output_2(self):
  304. """rv: [True, False]"""
  305. return self.get(self.KEY_OUTPUT_2)
  306. @property
  307. def output_3(self):
  308. """rv: [True, False]"""
  309. return self.get(self.KEY_OUTPUT_3)
  310. #
  311. # TX
  312. #
  313. def set_output(self, key, state):
  314. if key in self.KEY_OUTPUT_LIST:
  315. self.pack(key, state)
  316. else:
  317. logging.error("Unknown key to set the output!")
  318. def set_output_0(self, state):
  319. """state: [True, False, 'toggle']"""
  320. self.pack(self.KEY_OUTPUT_0, state)
  321. def set_output_0_mcb(self, device, key, data):
  322. logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
  323. self.set_output_0(data)
  324. def toggle_output_0_mcb(self, device, key, data):
  325. logger.info("%s: Toggeling output 0", self.topic)
  326. self.set_output_0('toggle')
  327. def set_output_1(self, state):
  328. """state: [True, False, 'toggle']"""
  329. self.pack(self.KEY_OUTPUT_1, state)
  330. def set_output_1_mcb(self, device, key, data):
  331. logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "%s: Changing output 1 to %s", self.topic, str(data))
  332. self.set_output_1(data)
  333. def toggle_output_1_mcb(self, device, key, data):
  334. logger.info("%s: Toggeling output 1", self.topic)
  335. self.set_output_1('toggle')
  336. def set_output_2(self, state):
  337. """state: [True, False, 'toggle']"""
  338. self.pack(self.KEY_OUTPUT_2, state)
  339. def set_output_2_mcb(self, device, key, data):
  340. logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "%s: Changing output 2 to %s", self.topic, str(data))
  341. self.set_output_2(data)
  342. def toggle_output_2_mcb(self, device, key, data):
  343. logger.info("%s: Toggeling output 2", self.topic)
  344. self.set_output_2('toggle')
  345. def set_output_3(self, state):
  346. """state: [True, False, 'toggle']"""
  347. self.pack(self.KEY_OUTPUT_3, state)
  348. def set_output_3_mcb(self, device, key, data):
  349. logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "%s: Changing output 3 to %s", self.topic, str(data))
  350. self.set_output_3(data)
  351. def toggle_output_3_mcb(self, device, key, data):
  352. logger.info("%s: Toggeling output 3", self.topic)
  353. self.set_output_3('toggle')
  354. def set_output_all(self, state):
  355. """state: [True, False, 'toggle']"""
  356. self.pack(self.KEY_OUTPUT_ALL, state)
  357. def set_output_all_mcb(self, device, key, data):
  358. logger.info("%s: Changing all outputs to %s", self.topic, str(data))
  359. self.set_output_all(data)
  360. def toggle_output_all_mcb(self, device, key, data):
  361. logger.info("%s: Toggeling all outputs", self.topic)
  362. self.set_output_0('toggle')
  363. class tradfri_light(base):
  364. KEY_LINKQUALITY = "linkquality"
  365. KEY_OUTPUT_0 = "state"
  366. KEY_BRIGHTNESS = "brightness"
  367. KEY_COLOR_TEMP = "color_temp"
  368. KEY_BRIGHTNESS_FADE = "brightness_move"
  369. #
  370. TX_TYPE = base.TX_DICT
  371. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  372. #
  373. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  374. RX_IGNORE_KEYS = ['update', 'color_mode']
  375. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  376. def __init__(self, mqtt_client, topic):
  377. super().__init__(mqtt_client, topic)
  378. def unpack_filter(self, key):
  379. if key == self.KEY_BRIGHTNESS:
  380. self[key] = (self[key] - 1) * 100 / 254
  381. elif key == self.KEY_COLOR_TEMP:
  382. self[key] = (self[key] - 250) * 10 / 204
  383. else:
  384. super().unpack_filter(key)
  385. def pack_filter(self, key, data):
  386. if key == self.KEY_BRIGHTNESS:
  387. return data * 254 / 100 + 1
  388. elif key == self.KEY_COLOR_TEMP:
  389. return data * 204 / 10 + 250
  390. else:
  391. return super().pack_filter(key, data)
  392. #
  393. # RX
  394. #
  395. @property
  396. def output_0(self):
  397. """rv: [True, False]"""
  398. return self.get(self.KEY_OUTPUT_0, False)
  399. @property
  400. def linkquality(self):
  401. """rv: numeric value"""
  402. return self.get(self.KEY_LINKQUALITY, 0)
  403. @property
  404. def brightness(self):
  405. """rv: numeric value [0%, ..., 100%]"""
  406. return self.get(self.KEY_BRIGHTNESS, 0)
  407. @property
  408. def color_temp(self):
  409. """rv: numeric value [0, ..., 10]"""
  410. return self.get(self.KEY_COLOR_TEMP, 0)
  411. #
  412. # TX
  413. #
  414. def set_output_0(self, state):
  415. """state: [True, False, 'toggle']"""
  416. self.pack(self.KEY_OUTPUT_0, state)
  417. def set_output_0_mcb(self, device, key, data):
  418. logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "%s: Changing output 0 to %s", self.topic, str(data))
  419. self.set_output_0(data)
  420. def toggle_output_0_mcb(self, device, key, data):
  421. logger.info("%s: Toggeling output 0", self.topic)
  422. self.set_output_0('toggle')
  423. def set_brightness(self, brightness):
  424. """brightness: [0, ..., 100]"""
  425. self.pack(self.KEY_BRIGHTNESS, brightness)
  426. def set_brightness_mcb(self, device, key, data):
  427. logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
  428. self.set_brightness(data)
  429. def default_inc(self, speed=40):
  430. self.pack(self.KEY_BRIGHTNESS_FADE, speed)
  431. def default_dec(self, speed=-40):
  432. self.default_inc(speed)
  433. def default_stop(self):
  434. self.default_inc(0)
  435. def set_color_temp(self, color_temp):
  436. """color_temp: [0, ..., 10]"""
  437. self.pack(self.KEY_COLOR_TEMP, color_temp)
  438. def set_color_temp_mcb(self, device, key, data):
  439. logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "%s: Changing color temperature to %s", self.topic, str(data))
  440. self.set_color_temp(data)
  441. class tradfri_button(base):
  442. ACTION_TOGGLE = "toggle"
  443. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  444. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  445. ACTION_RIGHT = "arrow_right_click"
  446. ACTION_LEFT = "arrow_left_click"
  447. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  448. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  449. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  450. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  451. ACTION_RIGHT_LONG = "arrow_right_hold"
  452. ACTION_RIGHT_RELEASE = "arrow_right_release"
  453. ACTION_LEFT_LONG = "arrow_left_hold"
  454. ACTION_LEFT_RELEASE = "arrow_left_release"
  455. #
  456. KEY_LINKQUALITY = "linkquality"
  457. KEY_BATTERY = "battery"
  458. KEY_ACTION = "action"
  459. KEY_ACTION_DURATION = "action_duration"
  460. #
  461. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  462. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  463. def __init__(self, mqtt_client, topic):
  464. super().__init__(mqtt_client, topic)
  465. #
  466. # RX
  467. #
  468. @property
  469. def action(self):
  470. """rv: action_txt"""
  471. return self.get(self.KEY_ACTION)
  472. #
  473. # WARNING CALL
  474. #
  475. def warning_call_condition(self):
  476. return self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
  477. def warning_text(self):
  478. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  479. class nodered_gui_heatvalve(base):
  480. KEY_FEEDBACK = "feedback"
  481. KEY_ENABLE = "enable"
  482. KEY_STATE = "state"
  483. KEY_BRIGHTNESS = "brightness"
  484. KEY_COLOR_TEMP = "color_temp"
  485. KEY_HEATING_BOOST = "heating_boost"
  486. KEY_HEATING_SETPOINT = "heating_setpoint"
  487. KEY_OFF_BUTTON = "off_button"
  488. #
  489. TX_TYPE = base.TX_VALUE
  490. #
  491. RX_KEYS = [KEY_STATE, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_HEATING_BOOST, KEY_HEATING_SETPOINT, KEY_OFF_BUTTON]
  492. def __init__(self, mqtt_client, topic):
  493. super().__init__(mqtt_client, topic)
  494. #
  495. # RX
  496. #
  497. @property
  498. def state(self):
  499. """rv: [True, False]"""
  500. return self.get(self.KEY_STATE)
  501. @property
  502. def brightness(self):
  503. """rv: [0, ..., 100]"""
  504. return self.get(self.KEY_BRIGHTNESS)
  505. @property
  506. def color_temp(self):
  507. """rv: [0, ..., 100]"""
  508. return self.get(self.KEY_COLOR_TEMP)
  509. @property
  510. def heating_boost(self):
  511. """rv: [True, False]"""
  512. return self.get(self.KEY_HEATING_BOOST)
  513. @property
  514. def heating_(self):
  515. """rv: [5, ..., 30]"""
  516. return self.get(self.KEY_HEATING_SETPOINT)
  517. @property
  518. def off_button(self):
  519. """rv: [True, False]"""
  520. return self.get(self.KEY_OFF_BUTTON)
  521. #
  522. # TX
  523. #
  524. def set_feedback(self, data):
  525. self.pack(self.KEY_FEEDBACK, data)
  526. class nodered_gui_button(base):
  527. KEY_STATE = "state"
  528. #
  529. RX_KEYS = [KEY_STATE]
  530. #
  531. # RX
  532. #
  533. @property
  534. def state(self):
  535. """rv: [True, False]"""
  536. return self.get(self.KEY_STATE)
  537. class nodered_gui_switch(nodered_gui_button):
  538. TX_TYPE = base.TX_VALUE
  539. #
  540. # TX
  541. #
  542. def set_state(self, data):
  543. """data: [True, False]"""
  544. self.pack(self.KEY_STATE, data)
  545. def set_state_mcb(self, device, key, data):
  546. logger.log(logging.INFO if data != self.get(self.KEY_STATE) else logging.DEBUG, "%s: Changing data to %s", self.topic, str(data))
  547. self.set_state(data)
  548. class nodered_gui_brightness_color_temp(base):
  549. KEY_ENABLE = "enable"
  550. KEY_BRIGHTNESS = "brightness"
  551. KEY_COLOR_TEMP = "color_temp"
  552. #
  553. TX_TYPE = base.TX_VALUE
  554. #
  555. RX_KEYS = [KEY_ENABLE, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  556. #
  557. # RX
  558. #
  559. @property
  560. def enable(self):
  561. """rv: [True, False]"""
  562. return self.get(self.KEY_ENABLE)
  563. @property
  564. def brightness(self):
  565. """rv: [True, False]"""
  566. return self.get(self.KEY_BRIGHTNESS)
  567. @property
  568. def color_temp(self):
  569. """rv: [True, False]"""
  570. return self.get(self.KEY_COLOR_TEMP)
  571. #
  572. # TX
  573. #
  574. def set_enable(self, data):
  575. """data: [True, False]"""
  576. self.pack(self.KEY_ENABLE, data)
  577. def set_enable_mcb(self, device, key, data):
  578. logger.log(logging.INFO if data != self.get(self.KEY_ENABLE) else logging.DEBUG, "%s: Changing enable to %s", self.topic, str(data))
  579. self.set_enable(data)
  580. def set_brightness(self, data):
  581. """data: [0%, ..., 100%]"""
  582. self.pack(self.KEY_BRIGHTNESS, data)
  583. def set_brightness_mcb(self, device, key, data):
  584. logger.log(logging.INFO if data != self.get(self.KEY_BRIGHTNESS) else logging.DEBUG, "%s: Changing brightness to %s", self.topic, str(data))
  585. self.set_brightness(data)
  586. def set_color_temp(self, data):
  587. """data: [0, ..., 10]"""
  588. self.pack(self.KEY_COLOR_TEMP, data)
  589. def set_color_temp_mcb(self, device, key, data):
  590. logger.log(logging.INFO if data != self.get(self.KEY_COLOR_TEMP) else logging.DEBUG,
  591. "%s: Changing color temperature to %s", self.topic, str(data))
  592. self.set_color_temp(data)
  593. class nodered_gui_leds(base):
  594. KEY_LED_0 = "led0"
  595. KEY_LED_1 = "led1"
  596. KEY_LED_2 = "led2"
  597. KEY_LED_3 = "led3"
  598. KEY_LED_4 = "led4"
  599. KEY_LED_5 = "led5"
  600. KEY_LED_6 = "led6"
  601. KEY_LED_7 = "led7"
  602. KEY_LED_8 = "led8"
  603. KEY_LED_9 = "led9"
  604. #
  605. TX_TYPE = base.TX_VALUE
  606. #
  607. RX_KEYS = [KEY_LED_0, KEY_LED_1, KEY_LED_2, KEY_LED_3, KEY_LED_4, KEY_LED_5, KEY_LED_6, KEY_LED_7, KEY_LED_8, KEY_LED_9]
  608. def set_led(self, key, data):
  609. """data: [True, False]"""
  610. logger.log(logging.INFO if data != self.get(key) else logging.DEBUG, "%s: Changing led state for %s to %s", self.topic, key, str(data))
  611. self.pack(key, data)
  612. class brennenstuhl_heatingvalve(base):
  613. KEY_LINKQUALITY = "linkquality"
  614. KEY_BATTERY = "battery"
  615. KEY_HEATING_SETPOINT = "current_heating_setpoint"
  616. KEY_TEMPERATURE = "local_temperature"
  617. #
  618. KEY_AWAY_MODE = "away_mode"
  619. KEY_CHILD_LOCK = "child_lock"
  620. KEY_PRESET = "preset"
  621. KEY_SYSTEM_MODE = "system_mode"
  622. KEY_VALVE_DETECTION = "valve_detection"
  623. KEY_WINDOW_DETECTION = "window_detection"
  624. #
  625. TX_TYPE = base.TX_DICT
  626. #
  627. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_HEATING_SETPOINT, KEY_TEMPERATURE]
  628. RX_IGNORE_KEYS = [KEY_AWAY_MODE, KEY_CHILD_LOCK, KEY_PRESET, KEY_SYSTEM_MODE, KEY_VALVE_DETECTION, KEY_WINDOW_DETECTION]
  629. def __init__(self, mqtt_client, topic):
  630. super().__init__(mqtt_client, topic)
  631. self.mqtt_client.send(self.topic + '/' + self.TX_TOPIC, json.dumps({self.KEY_WINDOW_DETECTION: "ON",
  632. self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat"}))
  633. def warning_call_condition(self):
  634. return self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
  635. def warning_text(self):
  636. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  637. #
  638. # RX
  639. #
  640. @property
  641. def linkqulity(self):
  642. return self.get(self.KEY_LINKQUALITY)
  643. @property
  644. def heating_setpoint(self):
  645. return self.get(self.KEY_HEATING_SETPOINT)
  646. @property
  647. def temperature(self):
  648. return self.get(self.KEY_TEMPERATURE)
  649. #
  650. # TX
  651. #
  652. def set_heating_setpoint(self, setpoint):
  653. self.pack(self.KEY_HEATING_SETPOINT, setpoint)
  654. def set_heating_setpoint_mcb(self, device, key, data):
  655. logger.info("%s: Changing heating setpoint to %s", self.topic, str(data))
  656. self.set_heating_setpoint(data)
  657. class remote(base):
  658. KEY_CD = "CD"
  659. KEY_LINE1 = "LINE1"
  660. KEY_LINE3 = "LINE3"
  661. KEY_MUTE = "MUTE"
  662. KEY_POWER = "POWER"
  663. KEY_VOLDOWN = "VOLDOWN"
  664. KEY_VOLUP = "VOLUP"
  665. #
  666. TX_TOPIC = ''
  667. TX_TYPE = base.TX_VALUE
  668. #
  669. RX_IGNORE_TOPICS = [KEY_CD, KEY_LINE1, KEY_LINE3, KEY_MUTE, KEY_POWER, KEY_VOLUP, KEY_VOLDOWN]
  670. def set_cd(self, device=None, key=None, data=None):
  671. self.pack(self.KEY_CD, None)
  672. def set_line1(self, device=None, key=None, data=None):
  673. self.pack(self.KEY_LINE1, None)
  674. def set_line3(self, device=None, key=None, data=None):
  675. self.pack(self.KEY_LINE3, None)
  676. def set_mute(self, device=None, key=None, data=None):
  677. self.pack(self.KEY_MUTE, None)
  678. def set_power(self, device=None, key=None, data=None):
  679. self.pack(self.KEY_POWER, None)
  680. def set_volume_up(self, data=False):
  681. """data: [True, False]"""
  682. self.pack(self.KEY_VOLUP, data)
  683. def set_volume_down(self, data=False):
  684. """data: [True, False]"""
  685. self.pack(self.KEY_VOLDOWN, data)
  686. def default_inc(self, device=None, key=None, data=None):
  687. self.set_volume_up(True)
  688. def default_dec(self, device=None, key=None, data=None):
  689. self.set_volume_down(True)
  690. def default_stop(self, device=None, key=None, data=None):
  691. self.set_volume_up(False)
  692. class status(base):
  693. KEY_STATE = "state"
  694. #
  695. TX_TYPE = base.TX_VALUE
  696. #
  697. RX_KEYS = [KEY_STATE]
  698. def set_state(self, num, data):
  699. """data: [True, False]"""
  700. self.pack(self.KEY_STATE + "/" + str(num), data)
  701. def set_state_mcb(self, device, key, data):
  702. logger.info("%s: Changing state to %s", self.topic, str(data))
  703. self.set_state(data)
  704. class audio_status(status):
  705. KEY_TITLE = "title"
  706. #
  707. RX_KEYS = [status.KEY_STATE, KEY_TITLE]