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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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_heatvalve(base):
  502. KEY_FEEDBACK = "feedback"
  503. KEY_ENABLE = "enable"
  504. KEY_STATE = "state"
  505. KEY_BRIGHTNESS = "brightness"
  506. KEY_COLOR_TEMP = "color_temp"
  507. KEY_HEATING_BOOST = "heating_boost"
  508. KEY_HEATING_SETPOINT = "heating_setpoint"
  509. KEY_OFF_BUTTON = "off_button"
  510. #
  511. TX_TYPE = base.TX_VALUE
  512. #
  513. RX_KEYS = [KEY_STATE, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_HEATING_BOOST, KEY_HEATING_SETPOINT, KEY_OFF_BUTTON]
  514. def __init__(self, mqtt_client, topic):
  515. super().__init__(mqtt_client, topic)
  516. #
  517. # RX
  518. #
  519. @property
  520. def state(self):
  521. """rv: [True, False]"""
  522. return self.get(self.KEY_STATE)
  523. @property
  524. def brightness(self):
  525. """rv: [0, ..., 100]"""
  526. return self.get(self.KEY_BRIGHTNESS)
  527. @property
  528. def color_temp(self):
  529. """rv: [0, ..., 100]"""
  530. return self.get(self.KEY_COLOR_TEMP)
  531. @property
  532. def heating_boost(self):
  533. """rv: [True, False]"""
  534. return self.get(self.KEY_HEATING_BOOST)
  535. @property
  536. def heating_(self):
  537. """rv: [5, ..., 30]"""
  538. return self.get(self.KEY_HEATING_SETPOINT)
  539. @property
  540. def off_button(self):
  541. """rv: [True, False]"""
  542. return self.get(self.KEY_OFF_BUTTON)
  543. #
  544. # TX
  545. #
  546. def set_feedback(self, data):
  547. self.pack(self.KEY_FEEDBACK, data)
  548. class nodered_gui_button(base):
  549. KEY_STATE = "state"
  550. #
  551. RX_KEYS = [KEY_STATE]
  552. #
  553. # RX
  554. #
  555. @property
  556. def state(self):
  557. """rv: [True, False]"""
  558. return self.get(self.KEY_STATE)
  559. class nodered_gui_switch(nodered_gui_button):
  560. TX_TYPE = base.TX_VALUE
  561. #
  562. # TX
  563. #
  564. def set_state(self, data):
  565. """data: [True, False]"""
  566. self.pack(self.KEY_STATE, data)
  567. def set_state_mcb(self, device, key, data):
  568. self.logger.debug("Sending %s with content %s", key, str(data))
  569. self.set_state(data)
  570. class nodered_gui_brightness_color_temp(base):
  571. KEY_ENABLE = "enable"
  572. KEY_BRIGHTNESS = "brightness"
  573. KEY_COLOR_TEMP = "color_temp"
  574. #
  575. TX_TYPE = base.TX_VALUE
  576. #
  577. RX_KEYS = [KEY_ENABLE, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  578. #
  579. # RX
  580. #
  581. @property
  582. def enable(self):
  583. """rv: [True, False]"""
  584. return self.get(self.KEY_ENABLE)
  585. @property
  586. def brightness(self):
  587. """rv: [True, False]"""
  588. return self.get(self.KEY_BRIGHTNESS)
  589. @property
  590. def color_temp(self):
  591. """rv: [True, False]"""
  592. return self.get(self.KEY_COLOR_TEMP)
  593. #
  594. # TX
  595. #
  596. def set_enable(self, data):
  597. """data: [True, False]"""
  598. self.pack(self.KEY_ENABLE, data)
  599. def set_enable_mcb(self, device, key, data):
  600. self.logger.debug("Sending %s with content %s", key, str(data))
  601. self.set_enable(data)
  602. def set_brightness(self, data):
  603. """data: [0%, ..., 100%]"""
  604. self.pack(self.KEY_BRIGHTNESS, data)
  605. def set_brightness_mcb(self, device, key, data):
  606. self.logger.debug("Sending %s with content %s", key, str(data))
  607. self.set_brightness(data)
  608. def set_color_temp(self, data):
  609. """data: [0, ..., 10]"""
  610. self.pack(self.KEY_COLOR_TEMP, data)
  611. def set_color_temp_mcb(self, device, key, data):
  612. self.logger.debug("Sending %s with content %s", key, str(data))
  613. self.set_color_temp(data)
  614. class nodered_gui_leds(base):
  615. KEY_LED_0 = "led0"
  616. KEY_LED_1 = "led1"
  617. KEY_LED_2 = "led2"
  618. KEY_LED_3 = "led3"
  619. KEY_LED_4 = "led4"
  620. KEY_LED_5 = "led5"
  621. KEY_LED_6 = "led6"
  622. KEY_LED_7 = "led7"
  623. KEY_LED_8 = "led8"
  624. KEY_LED_9 = "led9"
  625. #
  626. TX_TYPE = base.TX_VALUE
  627. #
  628. 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]
  629. def set_led(self, key, data):
  630. """data: [True, False]"""
  631. self.logger.debug("Sending %s with content %s", key, str(data))
  632. self.pack(key, data)
  633. class brennenstuhl_heatingvalve(base):
  634. KEY_LINKQUALITY = "linkquality"
  635. KEY_BATTERY = "battery"
  636. KEY_HEATING_SETPOINT = "current_heating_setpoint"
  637. KEY_TEMPERATURE = "local_temperature"
  638. #
  639. KEY_AWAY_MODE = "away_mode"
  640. KEY_CHILD_LOCK = "child_lock"
  641. KEY_PRESET = "preset"
  642. KEY_SYSTEM_MODE = "system_mode"
  643. KEY_VALVE_DETECTION = "valve_detection"
  644. KEY_WINDOW_DETECTION = "window_detection"
  645. #
  646. TX_TYPE = base.TX_DICT
  647. #
  648. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_HEATING_SETPOINT, KEY_TEMPERATURE]
  649. RX_IGNORE_KEYS = [KEY_AWAY_MODE, KEY_CHILD_LOCK, KEY_PRESET, KEY_SYSTEM_MODE, KEY_VALVE_DETECTION, KEY_WINDOW_DETECTION]
  650. def __init__(self, mqtt_client, topic):
  651. super().__init__(mqtt_client, topic)
  652. self.mqtt_client.send(self.topic + '/' + self.TX_TOPIC, json.dumps({self.KEY_WINDOW_DETECTION: "ON",
  653. self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat"}))
  654. def warning_call_condition(self):
  655. return self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
  656. def warning_text(self):
  657. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  658. #
  659. # RX
  660. #
  661. @property
  662. def linkqulity(self):
  663. return self.get(self.KEY_LINKQUALITY)
  664. @property
  665. def heating_setpoint(self):
  666. return self.get(self.KEY_HEATING_SETPOINT)
  667. @property
  668. def temperature(self):
  669. return self.get(self.KEY_TEMPERATURE)
  670. #
  671. # TX
  672. #
  673. def set_heating_setpoint(self, setpoint):
  674. self.pack(self.KEY_HEATING_SETPOINT, setpoint)
  675. def set_heating_setpoint_mcb(self, device, key, data):
  676. self.logger.info("Changing heating setpoint to %s", str(data))
  677. self.set_heating_setpoint(data)
  678. class remote(base):
  679. KEY_CD = "CD"
  680. KEY_LINE1 = "LINE1"
  681. KEY_LINE3 = "LINE3"
  682. KEY_MUTE = "MUTE"
  683. KEY_POWER = "POWER"
  684. KEY_VOLDOWN = "VOLDOWN"
  685. KEY_VOLUP = "VOLUP"
  686. #
  687. TX_TOPIC = ''
  688. TX_TYPE = base.TX_VALUE
  689. #
  690. RX_IGNORE_TOPICS = [KEY_CD, KEY_LINE1, KEY_LINE3, KEY_MUTE, KEY_POWER, KEY_VOLUP, KEY_VOLDOWN]
  691. def set_cd(self, device=None, key=None, data=None):
  692. self.pack(self.KEY_CD, None)
  693. def set_line1(self, device=None, key=None, data=None):
  694. self.pack(self.KEY_LINE1, None)
  695. def set_line3(self, device=None, key=None, data=None):
  696. self.pack(self.KEY_LINE3, None)
  697. def set_mute(self, device=None, key=None, data=None):
  698. self.pack(self.KEY_MUTE, None)
  699. def set_power(self, device=None, key=None, data=None):
  700. self.pack(self.KEY_POWER, None)
  701. def set_volume_up(self, data=False):
  702. """data: [True, False]"""
  703. self.pack(self.KEY_VOLUP, data)
  704. def set_volume_down(self, data=False):
  705. """data: [True, False]"""
  706. self.pack(self.KEY_VOLDOWN, data)
  707. def default_inc(self, device=None, key=None, data=None):
  708. self.set_volume_up(True)
  709. def default_dec(self, device=None, key=None, data=None):
  710. self.set_volume_down(True)
  711. def default_stop(self, device=None, key=None, data=None):
  712. self.set_volume_up(False)
  713. class status(base):
  714. KEY_STATE = "state"
  715. #
  716. TX_TYPE = base.TX_VALUE
  717. #
  718. RX_KEYS = [KEY_STATE]
  719. def set_state(self, num, data):
  720. """data: [True, False]"""
  721. self.pack(self.KEY_STATE + "/" + str(num), data)
  722. def set_state_mcb(self, device, key, data):
  723. self.logger.info("Changing state to %s", str(data))
  724. self.set_state(data)
  725. class audio_status(status):
  726. KEY_TITLE = "title"
  727. #
  728. RX_KEYS = [status.KEY_STATE, KEY_TITLE]