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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. BATTERY_WARN_LEVEL = 5
  26. def is_json(data):
  27. try:
  28. json.loads(data)
  29. except json.decoder.JSONDecodeError:
  30. return False
  31. else:
  32. return True
  33. class base(dict):
  34. TX_TOPIC = "set"
  35. TX_VALUE = 0
  36. TX_DICT = 1
  37. TX_TYPE = -1
  38. TX_FILTER_DATA_KEYS = []
  39. #
  40. RX_KEYS = []
  41. RX_IGNORE_TOPICS = []
  42. RX_IGNORE_KEYS = []
  43. RX_FILTER_DATA_KEYS = []
  44. def __init__(self, mqtt_client, topic):
  45. # data storage
  46. self.mqtt_client = mqtt_client
  47. self.topic = topic
  48. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  49. for entry in self.topic.split('/'):
  50. self.logger = self.logger.getChild(entry)
  51. # initialisations
  52. dict.__init__(self)
  53. mqtt_client.add_callback(
  54. topic=self.topic, callback=self.receive_callback)
  55. mqtt_client.add_callback(
  56. topic=self.topic+"/#", callback=self.receive_callback)
  57. #
  58. self.callback_list = []
  59. self.warning_callback = None
  60. #
  61. self.__previous__ = {}
  62. def receive_callback(self, client, userdata, message):
  63. self.unpack(message)
  64. def unpack_filter(self, key):
  65. if key in self.RX_FILTER_DATA_KEYS:
  66. if self.get(key) == 1 or self.get(key) == 'on' or self.get(key) == 'ON':
  67. self[key] = True
  68. elif self.get(key) == 0 or self.get(key) == 'off' or self.get(key) == 'OFF':
  69. self[key] = False
  70. def unpack_single_value(self, key, data):
  71. prev_value = self.get(key)
  72. if key in self.RX_KEYS:
  73. self[key] = data
  74. self.__previous__[key] = prev_value
  75. # Filter, if needed
  76. self.unpack_filter(key)
  77. self.logger.debug("Received data %s - %s", key, str(self.get(key)))
  78. self.callback_caller(key, self[key], self.get(key) != self.__previous__.get(key))
  79. elif key not in self.RX_IGNORE_KEYS:
  80. self.logger.warning('Got a message with unparsed content: "%s - %s"', key, str(data))
  81. else:
  82. self.logger.debug("Ignoring key %s", key)
  83. def unpack(self, message):
  84. content_key = message.topic[len(self.topic) + 1:]
  85. if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
  86. self.logger.debug("Unpacking content_key \"%s\" from message.", content_key)
  87. if is_json(message.payload):
  88. data = json.loads(message.payload)
  89. if type(data) is dict:
  90. for key in data:
  91. self.unpack_single_value(key, data[key])
  92. else:
  93. self.unpack_single_value(content_key, data)
  94. # String
  95. else:
  96. self.unpack_single_value(
  97. content_key, message.payload.decode('utf-8'))
  98. self.warning_caller()
  99. else:
  100. self.logger.debug("Ignoring topic %s", content_key)
  101. def pack_filter(self, key, data):
  102. if key in self.TX_FILTER_DATA_KEYS:
  103. if data is True:
  104. return "on"
  105. elif data is False:
  106. return "off"
  107. else:
  108. return data
  109. return data
  110. def set(self, key, data):
  111. self.pack(key, data)
  112. def pack(self, key, data):
  113. data = self.pack_filter(key, data)
  114. if self.TX_TOPIC is not None:
  115. if self.TX_TYPE < 0:
  116. self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value")
  117. else:
  118. self.logger.debug("Sending data for %s - %s", key, str(data))
  119. if self.TX_TYPE == self.TX_DICT:
  120. self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
  121. else:
  122. if type(data) not in [str, bytes]:
  123. data = json.dumps(data)
  124. self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
  125. else:
  126. self.logger.error("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. self.logger.warning(warn_txt)
  148. if self.warning_callback is not None:
  149. self.warning_callback(self, warn_txt)
  150. def warning_text(self):
  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. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  231. self.set_output_0(data)
  232. def toggle_output_0_mcb(self, device, key, data):
  233. self.logger.info("Toggeling output 0")
  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. self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "Changing output 1 to %s", str(data))
  240. self.set_output_1(data)
  241. def toggle_output_1_mcb(self, device, key, data):
  242. self.logger.info("Toggeling output 1")
  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. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  274. self.set_output_0(data)
  275. def toggle_output_0_mcb(self, device, key, data):
  276. self.logger.info("Toggeling output 0")
  277. self.set_output_0('toggle')
  278. class silvercrest_motion_sensor(base):
  279. KEY_BATTERY = "battery"
  280. KEY_BATTERY_LOW = "battery_low"
  281. KEY_LINKQUALITY = "linkquality"
  282. KEY_OCCUPANCY = "occupancy"
  283. KEY_UNMOUNTED = "tamper"
  284. KEY_VOLTAGE = "voltage"
  285. #
  286. RX_KEYS = [KEY_BATTERY, KEY_BATTERY_LOW, KEY_LINKQUALITY, KEY_OCCUPANCY, KEY_UNMOUNTED, KEY_VOLTAGE]
  287. def __init__(self, mqtt_client, topic):
  288. super().__init__(mqtt_client, topic)
  289. def warning_call_condition(self):
  290. return self.get(self.KEY_BATTERY_LOW)
  291. def warning_text(self, data):
  292. return "Battery low: level=%d" % self.get(self.KEY_BATTERY)
  293. #
  294. # RX
  295. #
  296. @property
  297. def linkquality(self):
  298. """rv: numeric value"""
  299. return self.get(self.KEY_LINKQUALITY)
  300. class my_powerplug(base):
  301. KEY_OUTPUT_0 = "output/1"
  302. KEY_OUTPUT_1 = "output/2"
  303. KEY_OUTPUT_2 = "output/3"
  304. KEY_OUTPUT_3 = "output/4"
  305. KEY_OUTPUT_ALL = "output/all"
  306. KEY_OUTPUT_LIST = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  307. #
  308. TX_TYPE = base.TX_VALUE
  309. #
  310. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  311. def __init__(self, mqtt_client, topic):
  312. super().__init__(mqtt_client, topic)
  313. #
  314. # RX
  315. #
  316. @property
  317. def output_0(self):
  318. """rv: [True, False]"""
  319. return self.get(self.KEY_OUTPUT_0)
  320. @property
  321. def output_1(self):
  322. """rv: [True, False]"""
  323. return self.get(self.KEY_OUTPUT_1)
  324. @property
  325. def output_2(self):
  326. """rv: [True, False]"""
  327. return self.get(self.KEY_OUTPUT_2)
  328. @property
  329. def output_3(self):
  330. """rv: [True, False]"""
  331. return self.get(self.KEY_OUTPUT_3)
  332. #
  333. # TX
  334. #
  335. def set_output(self, key, state):
  336. if key in self.KEY_OUTPUT_LIST:
  337. self.pack(key, state)
  338. else:
  339. logging.error("Unknown key to set the output!")
  340. def set_output_0(self, state):
  341. """state: [True, False, 'toggle']"""
  342. self.pack(self.KEY_OUTPUT_0, state)
  343. def set_output_0_mcb(self, device, key, data):
  344. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  345. self.set_output_0(data)
  346. def toggle_output_0_mcb(self, device, key, data):
  347. self.logger.info("Toggeling output 0")
  348. self.set_output_0('toggle')
  349. def set_output_1(self, state):
  350. """state: [True, False, 'toggle']"""
  351. self.pack(self.KEY_OUTPUT_1, state)
  352. def set_output_1_mcb(self, device, key, data):
  353. self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "Changing output 1 to %s", str(data))
  354. self.set_output_1(data)
  355. def toggle_output_1_mcb(self, device, key, data):
  356. self.logger.info("Toggeling output 1")
  357. self.set_output_1('toggle')
  358. def set_output_2(self, state):
  359. """state: [True, False, 'toggle']"""
  360. self.pack(self.KEY_OUTPUT_2, state)
  361. def set_output_2_mcb(self, device, key, data):
  362. self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "Changing output 2 to %s", str(data))
  363. self.set_output_2(data)
  364. def toggle_output_2_mcb(self, device, key, data):
  365. self.logger.info("Toggeling output 2")
  366. self.set_output_2('toggle')
  367. def set_output_3(self, state):
  368. """state: [True, False, 'toggle']"""
  369. self.pack(self.KEY_OUTPUT_3, state)
  370. def set_output_3_mcb(self, device, key, data):
  371. self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "Changing output 3 to %s", str(data))
  372. self.set_output_3(data)
  373. def toggle_output_3_mcb(self, device, key, data):
  374. self.logger.info("Toggeling output 3")
  375. self.set_output_3('toggle')
  376. def set_output_all(self, state):
  377. """state: [True, False, 'toggle']"""
  378. self.pack(self.KEY_OUTPUT_ALL, state)
  379. def set_output_all_mcb(self, device, key, data):
  380. self.logger.info("Changing all outputs to %s", str(data))
  381. self.set_output_all(data)
  382. def toggle_output_all_mcb(self, device, key, data):
  383. self.logger.info("Toggeling all outputs")
  384. self.set_output_0('toggle')
  385. class tradfri_light(base):
  386. KEY_LINKQUALITY = "linkquality"
  387. KEY_OUTPUT_0 = "state"
  388. KEY_BRIGHTNESS = "brightness"
  389. KEY_COLOR_TEMP = "color_temp"
  390. KEY_BRIGHTNESS_FADE = "brightness_move"
  391. #
  392. TX_TYPE = base.TX_DICT
  393. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  394. #
  395. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  396. RX_IGNORE_KEYS = ['update', 'color_mode']
  397. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  398. def __init__(self, mqtt_client, topic):
  399. super().__init__(mqtt_client, topic)
  400. def unpack_filter(self, key):
  401. if key == self.KEY_BRIGHTNESS:
  402. self[key] = (self[key] - 1) * 100 / 254
  403. elif key == self.KEY_COLOR_TEMP:
  404. self[key] = (self[key] - 250) * 10 / 204
  405. else:
  406. super().unpack_filter(key)
  407. def pack_filter(self, key, data):
  408. if key == self.KEY_BRIGHTNESS:
  409. return data * 254 / 100 + 1
  410. elif key == self.KEY_COLOR_TEMP:
  411. return data * 204 / 10 + 250
  412. else:
  413. return super().pack_filter(key, data)
  414. #
  415. # RX
  416. #
  417. @property
  418. def output_0(self):
  419. """rv: [True, False]"""
  420. return self.get(self.KEY_OUTPUT_0, False)
  421. @property
  422. def linkquality(self):
  423. """rv: numeric value"""
  424. return self.get(self.KEY_LINKQUALITY, 0)
  425. @property
  426. def brightness(self):
  427. """rv: numeric value [0%, ..., 100%]"""
  428. return self.get(self.KEY_BRIGHTNESS, 0)
  429. @property
  430. def color_temp(self):
  431. """rv: numeric value [0, ..., 10]"""
  432. return self.get(self.KEY_COLOR_TEMP, 0)
  433. #
  434. # TX
  435. #
  436. def set_output_0(self, state):
  437. """state: [True, False, 'toggle']"""
  438. self.pack(self.KEY_OUTPUT_0, state)
  439. def set_output_0_mcb(self, device, key, data):
  440. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  441. self.set_output_0(data)
  442. def toggle_output_0_mcb(self, device, key, data):
  443. self.logger.info("Toggeling output 0")
  444. self.set_output_0('toggle')
  445. def set_brightness(self, brightness):
  446. """brightness: [0, ..., 100]"""
  447. self.pack(self.KEY_BRIGHTNESS, brightness)
  448. def set_brightness_mcb(self, device, key, data):
  449. self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "Changing brightness to %s", str(data))
  450. self.set_brightness(data)
  451. def default_inc(self, speed=40):
  452. self.pack(self.KEY_BRIGHTNESS_FADE, speed)
  453. def default_dec(self, speed=-40):
  454. self.default_inc(speed)
  455. def default_stop(self):
  456. self.default_inc(0)
  457. def set_color_temp(self, color_temp):
  458. """color_temp: [0, ..., 10]"""
  459. self.pack(self.KEY_COLOR_TEMP, color_temp)
  460. def set_color_temp_mcb(self, device, key, data):
  461. self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "Changing color temperature to %s", str(data))
  462. self.set_color_temp(data)
  463. class tradfri_button(base):
  464. ACTION_TOGGLE = "toggle"
  465. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  466. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  467. ACTION_RIGHT = "arrow_right_click"
  468. ACTION_LEFT = "arrow_left_click"
  469. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  470. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  471. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  472. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  473. ACTION_RIGHT_LONG = "arrow_right_hold"
  474. ACTION_RIGHT_RELEASE = "arrow_right_release"
  475. ACTION_LEFT_LONG = "arrow_left_hold"
  476. ACTION_LEFT_RELEASE = "arrow_left_release"
  477. #
  478. KEY_LINKQUALITY = "linkquality"
  479. KEY_BATTERY = "battery"
  480. KEY_ACTION = "action"
  481. KEY_ACTION_DURATION = "action_duration"
  482. #
  483. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  484. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  485. def __init__(self, mqtt_client, topic):
  486. super().__init__(mqtt_client, topic)
  487. #
  488. # RX
  489. #
  490. @property
  491. def action(self):
  492. """rv: action_txt"""
  493. return self.get(self.KEY_ACTION)
  494. #
  495. # WARNING CALL
  496. #
  497. def warning_call_condition(self):
  498. return self.get(self.KEY_BATTERY) is not None and self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
  499. def warning_text(self):
  500. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  501. class nodered_gui_leds(base):
  502. KEY_LED_0 = "led0"
  503. KEY_LED_1 = "led1"
  504. KEY_LED_2 = "led2"
  505. KEY_LED_3 = "led3"
  506. KEY_LED_4 = "led4"
  507. KEY_LED_5 = "led5"
  508. KEY_LED_6 = "led6"
  509. KEY_LED_7 = "led7"
  510. KEY_LED_8 = "led8"
  511. KEY_LED_9 = "led9"
  512. KEY_LED_LIST = [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]
  513. #
  514. TX_TYPE = base.TX_VALUE
  515. def set_led(self, key, data):
  516. """data: [True, False]"""
  517. self.logger.debug("Sending %s with content %s", key, str(data))
  518. self.pack(key, data)
  519. class nodered_gui_timer(base):
  520. KEY_TIMER = "timer"
  521. #
  522. TX_TYPE = base.TX_VALUE
  523. def set_timer(self, data):
  524. """data: numeric"""
  525. self.pack(self.KEY_TIMER, data)
  526. def set_timer_mcb(self, device, key, data):
  527. self.logger.debug("Sending %s with content %s", key, str(data))
  528. self.set_timer(data)
  529. class nodered_gui_button(base):
  530. KEY_STATE = "state"
  531. #
  532. RX_KEYS = [KEY_STATE]
  533. #
  534. # RX
  535. #
  536. @property
  537. def state(self):
  538. """rv: [True, False]"""
  539. return self.get(self.KEY_STATE)
  540. class nodered_gui_switch(nodered_gui_button):
  541. TX_TYPE = base.TX_VALUE
  542. #
  543. # TX
  544. #
  545. def set_state(self, data):
  546. """data: [True, False]"""
  547. self.pack(self.KEY_STATE, data)
  548. def set_state_mcb(self, device, key, data):
  549. self.logger.debug("Sending %s with content %s", key, str(data))
  550. self.set_state(data)
  551. class nodered_gui_light(nodered_gui_switch, nodered_gui_leds, nodered_gui_timer):
  552. KEY_ENABLE = "enable"
  553. KEY_BRIGHTNESS = "brightness"
  554. KEY_COLOR_TEMP = "color_temp"
  555. #
  556. TX_TYPE = base.TX_VALUE
  557. #
  558. RX_KEYS = nodered_gui_switch.RX_KEYS + [KEY_ENABLE, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  559. #
  560. # RX
  561. #
  562. @property
  563. def enable(self):
  564. """rv: [True, False]"""
  565. return self.get(self.KEY_ENABLE)
  566. @property
  567. def brightness(self):
  568. """rv: [True, False]"""
  569. return self.get(self.KEY_BRIGHTNESS)
  570. @property
  571. def color_temp(self):
  572. """rv: [True, False]"""
  573. return self.get(self.KEY_COLOR_TEMP)
  574. #
  575. # TX
  576. #
  577. def set_enable(self, data):
  578. """data: [True, False]"""
  579. self.pack(self.KEY_ENABLE, data)
  580. def set_enable_mcb(self, device, key, data):
  581. self.logger.debug("Sending %s with content %s", key, str(data))
  582. self.set_enable(data)
  583. def set_brightness(self, data):
  584. """data: [0%, ..., 100%]"""
  585. self.pack(self.KEY_BRIGHTNESS, data)
  586. def set_brightness_mcb(self, device, key, data):
  587. self.logger.debug("Sending %s with content %s", key, str(data))
  588. self.set_brightness(data)
  589. def set_color_temp(self, data):
  590. """data: [0, ..., 10]"""
  591. self.pack(self.KEY_COLOR_TEMP, data)
  592. def set_color_temp_mcb(self, device, key, data):
  593. self.logger.debug("Sending %s with content %s", key, str(data))
  594. self.set_color_temp(data)
  595. class nodered_gui_radiator(nodered_gui_timer):
  596. KEY_TEMPERATURE = "temperature"
  597. KEY_SETPOINT_TEMP = "setpoint_temp"
  598. KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
  599. KEY_BOOST = 'boost'
  600. KEY_AWAY = "away"
  601. KEY_SUMMER = "summer"
  602. KEY_ENABLE = "enable"
  603. #
  604. RX_KEYS = [KEY_TEMPERATURE, KEY_SETPOINT_TEMP, KEY_SETPOINT_TO_DEFAULT, KEY_BOOST, KEY_AWAY, KEY_SUMMER]
  605. #
  606. # TX
  607. #
  608. def set_temperature(self, data):
  609. """data: [True, False]"""
  610. self.pack(self.KEY_TEMPERATURE, data)
  611. def set_temperature_mcb(self, device, key, data):
  612. self.logger.debug("Sending %s with content %s", key, str(data))
  613. self.set_temperature(data)
  614. def set_setpoint_temperature(self, data):
  615. """data: [True, False]"""
  616. self.pack(self.KEY_SETPOINT_TEMP, data)
  617. def set_setpoint_temperature_mcb(self, device, key, data):
  618. self.logger.debug("Sending %s with content %s", key, str(data))
  619. self.set_setpoint_temperature(data)
  620. def set_away(self, data):
  621. """data: [True, False]"""
  622. self.pack(self.KEY_AWAY, data)
  623. def set_away_mcb(self, device, key, data):
  624. self.logger.debug("Sending %s with content %s", key, str(data))
  625. self.set_away(data)
  626. def set_summer(self, data):
  627. """data: [True, False]"""
  628. self.pack(self.KEY_SUMMER, data)
  629. def set_summer_mcb(self, device, key, data):
  630. self.logger.debug("Sending %s with content %s", key, str(data))
  631. self.set_summer(data)
  632. def set_enable(self, data):
  633. """data: [True, False]"""
  634. self.pack(self.KEY_ENABLE, data)
  635. def set_enable_mcb(self, device, key, data):
  636. self.logger.debug("Sending %s with content %s", key, str(data))
  637. self.set_enable(data)
  638. class brennenstuhl_heatingvalve(base):
  639. KEY_LINKQUALITY = "linkquality"
  640. KEY_BATTERY = "battery"
  641. KEY_HEATING_SETPOINT = "current_heating_setpoint"
  642. KEY_TEMPERATURE = "local_temperature"
  643. #
  644. KEY_AWAY_MODE = "away_mode"
  645. KEY_CHILD_LOCK = "child_lock"
  646. KEY_PRESET = "preset"
  647. KEY_SYSTEM_MODE = "system_mode"
  648. KEY_VALVE_DETECTION = "valve_detection"
  649. KEY_WINDOW_DETECTION = "window_detection"
  650. #
  651. TX_TYPE = base.TX_DICT
  652. #
  653. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_HEATING_SETPOINT, KEY_TEMPERATURE]
  654. RX_IGNORE_KEYS = [KEY_AWAY_MODE, KEY_CHILD_LOCK, KEY_PRESET, KEY_SYSTEM_MODE, KEY_VALVE_DETECTION, KEY_WINDOW_DETECTION]
  655. def __init__(self, mqtt_client, topic):
  656. super().__init__(mqtt_client, topic)
  657. self.mqtt_client.send(self.topic + '/' + self.TX_TOPIC, json.dumps({self.KEY_WINDOW_DETECTION: "ON",
  658. self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat", self.KEY_PRESET: "manual"}))
  659. def warning_call_condition(self):
  660. return self.get(self.KEY_BATTERY, 100) <= BATTERY_WARN_LEVEL
  661. def warning_text(self):
  662. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  663. #
  664. # RX
  665. #
  666. @property
  667. def linkqulity(self):
  668. return self.get(self.KEY_LINKQUALITY)
  669. @property
  670. def heating_setpoint(self):
  671. return self.get(self.KEY_HEATING_SETPOINT)
  672. @property
  673. def temperature(self):
  674. return self.get(self.KEY_TEMPERATURE)
  675. #
  676. # TX
  677. #
  678. def set_heating_setpoint(self, setpoint):
  679. self.pack(self.KEY_HEATING_SETPOINT, setpoint)
  680. def set_heating_setpoint_mcb(self, device, key, data):
  681. self.logger.info("Changing heating setpoint to %s", str(data))
  682. self.set_heating_setpoint(data)
  683. class remote(base):
  684. KEY_CD = "CD"
  685. KEY_LINE1 = "LINE1"
  686. KEY_LINE3 = "LINE3"
  687. KEY_MUTE = "MUTE"
  688. KEY_POWER = "POWER"
  689. KEY_VOLDOWN = "VOLDOWN"
  690. KEY_VOLUP = "VOLUP"
  691. #
  692. TX_TOPIC = ''
  693. TX_TYPE = base.TX_VALUE
  694. #
  695. RX_IGNORE_TOPICS = [KEY_CD, KEY_LINE1, KEY_LINE3, KEY_MUTE, KEY_POWER, KEY_VOLUP, KEY_VOLDOWN]
  696. def set_cd(self, device=None, key=None, data=None):
  697. self.pack(self.KEY_CD, None)
  698. def set_line1(self, device=None, key=None, data=None):
  699. self.pack(self.KEY_LINE1, None)
  700. def set_line3(self, device=None, key=None, data=None):
  701. self.pack(self.KEY_LINE3, None)
  702. def set_mute(self, device=None, key=None, data=None):
  703. self.pack(self.KEY_MUTE, None)
  704. def set_power(self, device=None, key=None, data=None):
  705. self.pack(self.KEY_POWER, None)
  706. def set_volume_up(self, data=False):
  707. """data: [True, False]"""
  708. self.pack(self.KEY_VOLUP, data)
  709. def set_volume_down(self, data=False):
  710. """data: [True, False]"""
  711. self.pack(self.KEY_VOLDOWN, data)
  712. def default_inc(self, device=None, key=None, data=None):
  713. self.set_volume_up(True)
  714. def default_dec(self, device=None, key=None, data=None):
  715. self.set_volume_down(True)
  716. def default_stop(self, device=None, key=None, data=None):
  717. self.set_volume_up(False)
  718. class status(base):
  719. KEY_STATE = "state"
  720. #
  721. TX_TYPE = base.TX_VALUE
  722. #
  723. RX_KEYS = [KEY_STATE]
  724. def set_state(self, num, data):
  725. """data: [True, False]"""
  726. self.pack(self.KEY_STATE + "/" + str(num), data)
  727. def set_state_mcb(self, device, key, data):
  728. self.logger.info("Changing state to %s", str(data))
  729. self.set_state(data)
  730. class audio_status(status):
  731. KEY_TITLE = "title"
  732. #
  733. RX_KEYS = [status.KEY_STATE, KEY_TITLE]