Smarthome Functionen
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

__init__.py 26KB

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