Smarthome Functionen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. from base import mqtt_base
  19. from function.videv import base as videv_base
  20. import json
  21. import logging
  22. import task
  23. try:
  24. from config import APP_NAME as ROOT_LOGGER_NAME
  25. except ImportError:
  26. ROOT_LOGGER_NAME = 'root'
  27. BATTERY_WARN_LEVEL = 5
  28. def is_json(data):
  29. try:
  30. json.loads(data)
  31. except json.decoder.JSONDecodeError:
  32. return False
  33. else:
  34. return True
  35. class group(object):
  36. def __init__(self, *args):
  37. super().__init__()
  38. self._members = args
  39. self._iter_counter = 0
  40. #
  41. self.methods = []
  42. for method in [m for m in args[0].__class__.__dict__.keys()]:
  43. if not method.startswith('_') and callable(getattr(args[0], method)): # add all public callable attributes to the list
  44. self.methods.append(method)
  45. #
  46. for member in self:
  47. methods = [m for m in member.__class__.__dict__.keys() if not m.startswith(
  48. '_') if not m.startswith('_') and callable(getattr(args[0], m))]
  49. if self.methods != methods:
  50. raise ValueError("All given instances needs to have same attributes:", self.methods, methods)
  51. def __iter__(self):
  52. return self
  53. def __next__(self):
  54. if self._iter_counter < len(self):
  55. self._iter_counter += 1
  56. return self._members[self._iter_counter - 1]
  57. self._iter_counter = 0
  58. raise StopIteration
  59. def __getitem__(self, i):
  60. return self._members[i]
  61. def __len__(self):
  62. return len(self._members)
  63. def __getattribute__(self, name):
  64. def group_execution(*args, **kwargs):
  65. for member in self[:]:
  66. m = getattr(member, name)
  67. m(*args, **kwargs)
  68. try:
  69. rv = super().__getattribute__(name)
  70. except AttributeError:
  71. return group_execution
  72. else:
  73. return rv
  74. class base(mqtt_base):
  75. TX_TOPIC = "set"
  76. TX_VALUE = 0
  77. TX_DICT = 1
  78. TX_TYPE = -1
  79. TX_FILTER_DATA_KEYS = []
  80. #
  81. RX_KEYS = []
  82. RX_IGNORE_TOPICS = []
  83. RX_IGNORE_KEYS = []
  84. RX_FILTER_DATA_KEYS = []
  85. #
  86. KEY_WARNING = '__WARNING__'
  87. def __init__(self, mqtt_client, topic):
  88. super().__init__(mqtt_client, topic, default_values=dict.fromkeys(self.RX_KEYS))
  89. # data storage
  90. # initialisations
  91. mqtt_client.add_callback(topic=self.topic, callback=self.receive_callback)
  92. mqtt_client.add_callback(topic=self.topic+"/#", callback=self.receive_callback)
  93. def set(self, key, data, block_callback=[]):
  94. if key in self.RX_IGNORE_KEYS:
  95. pass # ignore these keys
  96. elif key in self.RX_KEYS:
  97. return super().set(key, data, block_callback)
  98. else:
  99. self.logger.warning("Unexpected key %s", key)
  100. def receive_callback(self, client, userdata, message):
  101. if message.topic != self.topic + '/' + videv_base.KEY_INFO:
  102. content_key = message.topic[len(self.topic) + 1:]
  103. if content_key not in self.RX_IGNORE_TOPICS and (not message.topic.endswith(self.TX_TOPIC) or len(self.TX_TOPIC) == 0):
  104. self.logger.debug("Unpacking content_key \"%s\" from message.", content_key)
  105. if is_json(message.payload):
  106. data = json.loads(message.payload)
  107. if type(data) is dict:
  108. for key in data:
  109. self.set(key, self.__device_to_instance_filter__(key, data[key]))
  110. else:
  111. self.set(content_key, self.__device_to_instance_filter__(content_key, data))
  112. # String
  113. else:
  114. self.set(content_key, self.__device_to_instance_filter__(content_key, message.payload.decode('utf-8')))
  115. else:
  116. self.logger.debug("Ignoring topic %s", content_key)
  117. def __device_to_instance_filter__(self, key, data):
  118. if key in self.RX_FILTER_DATA_KEYS:
  119. if data in [1, 'on', 'ON']:
  120. return True
  121. elif data in [0, 'off', 'OFF']:
  122. return False
  123. return data
  124. def __instance_to_device_filter__(self, key, data):
  125. if key in self.TX_FILTER_DATA_KEYS:
  126. if data is True:
  127. return "on"
  128. elif data is False:
  129. return "off"
  130. return data
  131. def send_command(self, key, data):
  132. data = self.__instance_to_device_filter__(key, data)
  133. if self.TX_TOPIC is not None:
  134. if self.TX_TYPE < 0:
  135. self.logger.error("Unknown tx type. Set TX_TYPE of class to a known value")
  136. else:
  137. self.logger.debug("Sending data for %s - %s", key, str(data))
  138. if self.TX_TYPE == self.TX_DICT:
  139. self.mqtt_client.send('/'.join([self.topic, self.TX_TOPIC]), json.dumps({key: data}))
  140. else:
  141. if type(data) not in [str, bytes]:
  142. data = json.dumps(data)
  143. self.mqtt_client.send('/'.join([self.topic, key, self.TX_TOPIC] if len(self.TX_TOPIC) > 0 else [self.topic, key]), data)
  144. else:
  145. self.logger.error("Unknown tx toptic. Set TX_TOPIC of class to a known value")
  146. class shelly(base):
  147. KEY_OUTPUT_0 = "relay/0"
  148. KEY_OUTPUT_1 = "relay/1"
  149. KEY_INPUT_0 = "input/0"
  150. KEY_INPUT_1 = "input/1"
  151. KEY_LONGPUSH_0 = "longpush/0"
  152. KEY_LONGPUSH_1 = "longpush/1"
  153. KEY_TEMPERATURE = "temperature"
  154. KEY_OVERTEMPERATURE = "overtemperature"
  155. KEY_ID = "id"
  156. KEY_MODEL = "model"
  157. KEY_MAC = "mac"
  158. KEY_IP = "ip"
  159. KEY_NEW_FIRMWARE = "new_fw"
  160. KEY_FIRMWARE_VERSION = "fw_ver"
  161. #
  162. TX_TOPIC = "command"
  163. TX_TYPE = base.TX_VALUE
  164. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1]
  165. #
  166. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OVERTEMPERATURE, KEY_TEMPERATURE,
  167. KEY_ID, KEY_MODEL, KEY_MAC, KEY_IP, KEY_NEW_FIRMWARE, KEY_FIRMWARE_VERSION]
  168. RX_IGNORE_TOPICS = [KEY_OUTPUT_0 + '/' + "energy", KEY_OUTPUT_1 + '/' + "energy", 'input_event/0', 'input_event/1']
  169. RX_IGNORE_KEYS = ['temperature_f']
  170. RX_FILTER_DATA_KEYS = [KEY_INPUT_0, KEY_INPUT_1, KEY_LONGPUSH_0, KEY_LONGPUSH_1, KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OVERTEMPERATURE]
  171. def __init__(self, mqtt_client, topic):
  172. super().__init__(mqtt_client, topic)
  173. #
  174. self.output_key_delayed = None
  175. self.delayed_flash_task = task.delayed(0.3, self.flash_task)
  176. self.delayed_off_task = task.delayed(0.3, self.off_task)
  177. #
  178. self.add_callback(self.KEY_OVERTEMPERATURE, True, self.__warning__, True)
  179. #
  180. self.all_off_requested = False
  181. def flash_task(self, *args):
  182. if self.flash_active:
  183. self.send_command(self.output_key_delayed, not self.get(self.output_key_delayed))
  184. self.output_key_delayed = None
  185. if self.all_off_requested:
  186. self.delayed_off_task.run()
  187. def off_task(self, *args):
  188. self.all_off()
  189. @property
  190. def flash_active(self):
  191. return self.output_key_delayed is not None
  192. #
  193. # WARNING CALL
  194. #
  195. def __warning__(self, client, key, data):
  196. pass # TODO: implement warning feedback (key: KEY_OVERTEMPERATURE - info: KEY_TEMPERATURE)
  197. #
  198. # RX
  199. #
  200. @property
  201. def output_0(self):
  202. """rv: [True, False]"""
  203. return self.get(self.KEY_OUTPUT_0)
  204. @property
  205. def output_1(self):
  206. """rv: [True, False]"""
  207. return self.get(self.KEY_OUTPUT_1)
  208. @property
  209. def input_0(self):
  210. """rv: [True, False]"""
  211. return self.get(self.KEY_INPUT_0)
  212. @property
  213. def input_1(self):
  214. """rv: [True, False]"""
  215. return self.get(self.KEY_INPUT_1)
  216. @property
  217. def longpush_0(self):
  218. """rv: [True, False]"""
  219. return self.get(self.KEY_LONGPUSH_0)
  220. @property
  221. def longpush_1(self):
  222. """rv: [True, False]"""
  223. return self.get(self.KEY_LONGPUSH_1)
  224. @property
  225. def temperature(self):
  226. """rv: numeric value"""
  227. return self.get(self.KEY_TEMPERATURE)
  228. #
  229. # TX
  230. #
  231. def set_output_0(self, state):
  232. """state: [True, False]"""
  233. self.send_command(self.KEY_OUTPUT_0, state)
  234. def set_output_0_mcb(self, device, key, data):
  235. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  236. self.set_output_0(data)
  237. def toggle_output_0_mcb(self, device, key, data):
  238. self.logger.info("Toggeling output 0")
  239. self.set_output_0(not self.output_0)
  240. def set_output_1(self, state):
  241. """state: [True, False]"""
  242. self.send_command(self.KEY_OUTPUT_1, state)
  243. def set_output_1_mcb(self, device, key, data):
  244. self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "Changing output 1 to %s", str(data))
  245. self.set_output_1(data)
  246. def toggle_output_1_mcb(self, device, key, data):
  247. self.logger.info("Toggeling output 1")
  248. self.set_output_1(not self.output_1)
  249. def flash_0_mcb(self, device, key, data):
  250. self.output_key_delayed = self.KEY_OUTPUT_0
  251. self.toggle_output_0_mcb(device, key, data)
  252. self.delayed_flash_task.run()
  253. def flash_1_mcb(self, device, key, data):
  254. self.output_key_delayed = self.KEY_OUTPUT_1
  255. self.toggle_output_1_mcb(device, key, data)
  256. self.delayed_flash_task.run()
  257. def all_off(self):
  258. if self.flash_active:
  259. self.all_off_requested = True
  260. else:
  261. if self.output_0:
  262. self.set_output_0(False)
  263. if self.output_1:
  264. self.set_output_1(False)
  265. class silvercrest_powerplug(base):
  266. KEY_LINKQUALITY = "linkquality"
  267. KEY_OUTPUT_0 = "state"
  268. #
  269. TX_TYPE = base.TX_DICT
  270. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  271. #
  272. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0]
  273. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  274. def __init__(self, mqtt_client, topic):
  275. super().__init__(mqtt_client, topic)
  276. #
  277. # RX
  278. #
  279. @property
  280. def output_0(self):
  281. """rv: [True, False]"""
  282. return self.get(self.KEY_OUTPUT_0)
  283. @property
  284. def linkquality(self):
  285. """rv: numeric value"""
  286. return self.get(self.KEY_LINKQUALITY)
  287. #
  288. # TX
  289. #
  290. def set_output_0(self, state):
  291. """state: [True, False]"""
  292. self.send_command(self.KEY_OUTPUT_0, state)
  293. def set_output_0_mcb(self, device, key, data):
  294. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  295. self.set_output_0(data)
  296. def toggle_output_0_mcb(self, device, key, data):
  297. self.logger.info("Toggeling output 0")
  298. self.set_output_0(not self.output_0)
  299. def all_off(self):
  300. if self.output_0:
  301. self.set_output_0(False)
  302. class silvercrest_motion_sensor(base):
  303. KEY_BATTERY = "battery"
  304. KEY_BATTERY_LOW = "battery_low"
  305. KEY_LINKQUALITY = "linkquality"
  306. KEY_OCCUPANCY = "occupancy"
  307. KEY_UNMOUNTED = "tamper"
  308. KEY_VOLTAGE = "voltage"
  309. #
  310. RX_KEYS = [KEY_BATTERY, KEY_BATTERY_LOW, KEY_LINKQUALITY, KEY_OCCUPANCY, KEY_UNMOUNTED, KEY_VOLTAGE]
  311. def __init__(self, mqtt_client, topic):
  312. super().__init__(mqtt_client, topic)
  313. #
  314. self.add_callback(self.KEY_BATTERY_LOW, True, self.__warning__, True)
  315. #
  316. # WARNING CALL
  317. #
  318. def __warning__(self, client, key, data):
  319. pass # TODO: implement warning feedback (key: KEY_BATTERY_LOW - info: KEY_BATTERY)
  320. #
  321. # RX
  322. #
  323. @property
  324. def linkquality(self):
  325. """rv: numeric value"""
  326. return self.get(self.KEY_LINKQUALITY)
  327. class my_powerplug(base):
  328. KEY_OUTPUT_0 = "output/1"
  329. KEY_OUTPUT_1 = "output/2"
  330. KEY_OUTPUT_2 = "output/3"
  331. KEY_OUTPUT_3 = "output/4"
  332. KEY_OUTPUT_ALL = "output/all"
  333. KEY_OUTPUT_LIST = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  334. #
  335. TX_TYPE = base.TX_VALUE
  336. #
  337. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  338. def __init__(self, mqtt_client, topic):
  339. super().__init__(mqtt_client, topic)
  340. #
  341. # RX
  342. #
  343. @property
  344. def output_0(self):
  345. """rv: [True, False]"""
  346. return self.get(self.KEY_OUTPUT_0)
  347. @property
  348. def output_1(self):
  349. """rv: [True, False]"""
  350. return self.get(self.KEY_OUTPUT_1)
  351. @property
  352. def output_2(self):
  353. """rv: [True, False]"""
  354. return self.get(self.KEY_OUTPUT_2)
  355. @property
  356. def output_3(self):
  357. """rv: [True, False]"""
  358. return self.get(self.KEY_OUTPUT_3)
  359. #
  360. # TX
  361. #
  362. def set_output(self, key, state):
  363. if key in self.KEY_OUTPUT_LIST:
  364. self.send_command(key, state)
  365. else:
  366. logging.error("Unknown key to set the output!")
  367. def set_output_0(self, state):
  368. """state: [True, False]"""
  369. self.send_command(self.KEY_OUTPUT_0, state)
  370. def set_output_0_mcb(self, device, key, data):
  371. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  372. self.set_output_0(data)
  373. def toggle_output_0_mcb(self, device, key, data):
  374. self.logger.info("Toggeling output 0")
  375. self.set_output_0(not self.output_0)
  376. def set_output_1(self, state):
  377. """state: [True, False]"""
  378. self.send_command(self.KEY_OUTPUT_1, state)
  379. def set_output_1_mcb(self, device, key, data):
  380. self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "Changing output 1 to %s", str(data))
  381. self.set_output_1(data)
  382. def toggle_output_1_mcb(self, device, key, data):
  383. self.logger.info("Toggeling output 1")
  384. self.set_output_1(not self.output_1)
  385. def set_output_2(self, state):
  386. """state: [True, False]"""
  387. self.send_command(self.KEY_OUTPUT_2, state)
  388. def set_output_2_mcb(self, device, key, data):
  389. self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "Changing output 2 to %s", str(data))
  390. self.set_output_2(data)
  391. def toggle_output_2_mcb(self, device, key, data):
  392. self.logger.info("Toggeling output 2")
  393. self.set_output_2(not self.output_2)
  394. def set_output_3(self, state):
  395. """state: [True, False]"""
  396. self.send_command(self.KEY_OUTPUT_3, state)
  397. def set_output_3_mcb(self, device, key, data):
  398. self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "Changing output 3 to %s", str(data))
  399. self.set_output_3(data)
  400. def toggle_output_3_mcb(self, device, key, data):
  401. self.logger.info("Toggeling output 3")
  402. self.set_output_3(not self.output_3)
  403. def set_output_all(self, state):
  404. """state: [True, False, 'toggle']"""
  405. self.send_command(self.KEY_OUTPUT_ALL, state)
  406. def set_output_all_mcb(self, device, key, data):
  407. self.logger.info("Changing all outputs to %s", str(data))
  408. self.set_output_all(data)
  409. def all_off(self):
  410. self.set_output_all(False)
  411. class tradfri_light(base):
  412. KEY_LINKQUALITY = "linkquality"
  413. KEY_OUTPUT_0 = "state"
  414. KEY_BRIGHTNESS = "brightness"
  415. KEY_COLOR_TEMP = "color_temp"
  416. KEY_BRIGHTNESS_FADE = "brightness_move"
  417. #
  418. TX_TYPE = base.TX_DICT
  419. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  420. #
  421. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  422. RX_IGNORE_KEYS = ['update', 'color_mode', 'color_temp_startup']
  423. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  424. def __init__(self, mqtt_client, topic):
  425. super().__init__(mqtt_client, topic)
  426. def __device_to_instance_filter__(self, key, data):
  427. if key == self.KEY_BRIGHTNESS:
  428. return int(round((data - 1) * 100 / 253, 0))
  429. elif key == self.KEY_COLOR_TEMP:
  430. return int(round((data - 250) * 10 / 204, 0))
  431. return super().__device_to_instance_filter__(key, data)
  432. def __instance_to_device_filter__(self, key, data):
  433. if key == self.KEY_BRIGHTNESS:
  434. return int(round(data * 253 / 100 + 1, 0))
  435. elif key == self.KEY_COLOR_TEMP:
  436. return int(round(data * 204 / 10 + 250, 0))
  437. return super().__instance_to_device_filter__(key, data)
  438. #
  439. # RX
  440. #
  441. @property
  442. def output_0(self):
  443. """rv: [True, False]"""
  444. return self.get(self.KEY_OUTPUT_0, False)
  445. @property
  446. def linkquality(self):
  447. """rv: numeric value"""
  448. return self.get(self.KEY_LINKQUALITY, 0)
  449. @property
  450. def brightness(self):
  451. """rv: numeric value [0%, ..., 100%]"""
  452. return self.get(self.KEY_BRIGHTNESS, 0)
  453. @property
  454. def color_temp(self):
  455. """rv: numeric value [0, ..., 10]"""
  456. return self.get(self.KEY_COLOR_TEMP, 0)
  457. #
  458. # TX
  459. #
  460. def request_data(self, device=None, key=None, data=None):
  461. self.mqtt_client.send(self.topic + "/get", '{"%s": ""}' % self.KEY_OUTPUT_0)
  462. def set_output_0(self, state):
  463. """state: [True, False]"""
  464. self.send_command(self.KEY_OUTPUT_0, state)
  465. def set_output_0_mcb(self, device, key, data):
  466. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  467. self.set_output_0(data)
  468. def toggle_output_0_mcb(self, device, key, data):
  469. self.logger.info("Toggeling output 0")
  470. self.set_output_0(not self.output_0)
  471. def set_brightness(self, brightness):
  472. """brightness: [0, ..., 100]"""
  473. self.send_command(self.KEY_BRIGHTNESS, brightness)
  474. def set_brightness_mcb(self, device, key, data):
  475. self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "Changing brightness to %s", str(data))
  476. self.set_brightness(data)
  477. def default_inc(self, speed=40):
  478. self.send_command(self.KEY_BRIGHTNESS_FADE, speed)
  479. def default_dec(self, speed=-40):
  480. self.default_inc(speed)
  481. def default_stop(self):
  482. self.default_inc(0)
  483. def set_color_temp(self, color_temp):
  484. """color_temp: [0, ..., 10]"""
  485. self.send_command(self.KEY_COLOR_TEMP, color_temp)
  486. def set_color_temp_mcb(self, device, key, data):
  487. self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "Changing color temperature to %s", str(data))
  488. self.set_color_temp(data)
  489. def all_off(self):
  490. if self.output_0:
  491. self.set_output_0(False)
  492. class tradfri_button(base):
  493. ACTION_TOGGLE = "toggle"
  494. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  495. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  496. ACTION_RIGHT = "arrow_right_click"
  497. ACTION_LEFT = "arrow_left_click"
  498. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  499. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  500. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  501. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  502. ACTION_RIGHT_LONG = "arrow_right_hold"
  503. ACTION_RIGHT_RELEASE = "arrow_right_release"
  504. ACTION_LEFT_LONG = "arrow_left_hold"
  505. ACTION_LEFT_RELEASE = "arrow_left_release"
  506. #
  507. KEY_LINKQUALITY = "linkquality"
  508. KEY_BATTERY = "battery"
  509. KEY_ACTION = "action"
  510. KEY_ACTION_DURATION = "action_duration"
  511. #
  512. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  513. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  514. def __init__(self, mqtt_client, topic):
  515. super().__init__(mqtt_client, topic)
  516. #
  517. self.add_callback(self.KEY_BATTERY, None, self.__warning__, True)
  518. self.__battery_warning__ = False
  519. #
  520. # WARNING CALL
  521. #
  522. def __warning__(self, client, key, data):
  523. if data <= BATTERY_WARN_LEVEL:
  524. if not self.__battery_warning__:
  525. self.__battery_warning__ = True
  526. pass # TODO: implement warning feedback (key: KEY_BATTERY_LOW - info: KEY_BATTERY)
  527. else:
  528. self.__battery_warning__ = False
  529. #
  530. # RX
  531. #
  532. @property
  533. def action(self):
  534. """rv: action_txt"""
  535. return self.get(self.KEY_ACTION)
  536. class brennenstuhl_heatingvalve(base):
  537. KEY_LINKQUALITY = "linkquality"
  538. KEY_BATTERY = "battery"
  539. KEY_HEATING_SETPOINT = "current_heating_setpoint"
  540. KEY_TEMPERATURE = "local_temperature"
  541. #
  542. KEY_AWAY_MODE = "away_mode"
  543. KEY_CHILD_LOCK = "child_lock"
  544. KEY_PRESET = "preset"
  545. KEY_SYSTEM_MODE = "system_mode"
  546. KEY_VALVE_DETECTION = "valve_detection"
  547. KEY_WINDOW_DETECTION = "window_detection"
  548. #
  549. TX_TYPE = base.TX_DICT
  550. #
  551. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_HEATING_SETPOINT, KEY_TEMPERATURE]
  552. RX_IGNORE_KEYS = [KEY_AWAY_MODE, KEY_CHILD_LOCK, KEY_PRESET, KEY_SYSTEM_MODE, KEY_VALVE_DETECTION, KEY_WINDOW_DETECTION]
  553. def __init__(self, mqtt_client, topic):
  554. super().__init__(mqtt_client, topic)
  555. self.mqtt_client.send(self.topic + '/' + self.TX_TOPIC, json.dumps({self.KEY_WINDOW_DETECTION: "ON",
  556. self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat", self.KEY_PRESET: "manual"}))
  557. #
  558. self.add_callback(self.KEY_BATTERY, None, self.__warning__, True)
  559. self.__battery_warning__ = False
  560. #
  561. # WARNING CALL
  562. #
  563. def __warning__(self, client, key, data):
  564. if data <= BATTERY_WARN_LEVEL:
  565. if not self.__battery_warning__:
  566. self.__battery_warning__ = True
  567. pass # TODO: implement warning feedback (key: KEY_BATTERY_LOW - info: KEY_BATTERY)
  568. else:
  569. self.__battery_warning__ = False
  570. #
  571. # RX
  572. #
  573. @property
  574. def linkqulity(self):
  575. return self.get(self.KEY_LINKQUALITY)
  576. @property
  577. def heating_setpoint(self):
  578. return self.get(self.KEY_HEATING_SETPOINT)
  579. @property
  580. def temperature(self):
  581. return self.get(self.KEY_TEMPERATURE)
  582. #
  583. # TX
  584. #
  585. def set_heating_setpoint(self, setpoint):
  586. self.send_command(self.KEY_HEATING_SETPOINT, setpoint)
  587. def set_heating_setpoint_mcb(self, device, key, data):
  588. self.logger.info("Changing heating setpoint to %s", str(data))
  589. self.set_heating_setpoint(data)
  590. class remote(base):
  591. KEY_CD = "CD"
  592. KEY_LINE1 = "LINE1"
  593. KEY_LINE3 = "LINE3"
  594. KEY_MUTE = "MUTE"
  595. KEY_POWER = "POWER"
  596. KEY_VOLDOWN = "VOLDOWN"
  597. KEY_VOLUP = "VOLUP"
  598. #
  599. TX_TOPIC = ''
  600. TX_TYPE = base.TX_VALUE
  601. #
  602. RX_IGNORE_TOPICS = [KEY_CD, KEY_LINE1, KEY_LINE3, KEY_MUTE, KEY_POWER, KEY_VOLUP, KEY_VOLDOWN]
  603. def set_cd(self, device=None, key=None, data=None):
  604. self.send_command(self.KEY_CD, None)
  605. def set_line1(self, device=None, key=None, data=None):
  606. self.send_command(self.KEY_LINE1, None)
  607. def set_line3(self, device=None, key=None, data=None):
  608. self.send_command(self.KEY_LINE3, None)
  609. def set_mute(self, device=None, key=None, data=None):
  610. self.send_command(self.KEY_MUTE, None)
  611. def set_power(self, device=None, key=None, data=None):
  612. self.send_command(self.KEY_POWER, None)
  613. def set_volume_up(self, data=False):
  614. """data: [True, False]"""
  615. self.send_command(self.KEY_VOLUP, data)
  616. def set_volume_down(self, data=False):
  617. """data: [True, False]"""
  618. self.send_command(self.KEY_VOLDOWN, data)
  619. def default_inc(self, device=None, key=None, data=None):
  620. self.set_volume_up(True)
  621. def default_dec(self, device=None, key=None, data=None):
  622. self.set_volume_down(True)
  623. def default_stop(self, device=None, key=None, data=None):
  624. self.set_volume_up(False)
  625. class status(base):
  626. KEY_STATE = "state"
  627. #
  628. TX_TYPE = base.TX_VALUE
  629. #
  630. RX_KEYS = [KEY_STATE]
  631. def set_state(self, num, data):
  632. """data: [True, False]"""
  633. self.send_command(self.KEY_STATE + "/" + str(num), data)
  634. def set_state_mcb(self, device, key, data):
  635. self.logger.info("Changing state to %s", str(data))
  636. self.set_state(data)
  637. class audio_status(status):
  638. KEY_TITLE = "title"
  639. #
  640. RX_KEYS = [status.KEY_STATE, KEY_TITLE]