Smarthome Functionen
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

__init__.py 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. self.set_output_0(False)
  313. self.set_output_1(False)
  314. class silvercrest_powerplug(base):
  315. KEY_LINKQUALITY = "linkquality"
  316. KEY_OUTPUT_0 = "state"
  317. #
  318. TX_TYPE = base.TX_DICT
  319. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  320. #
  321. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0]
  322. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  323. def __init__(self, mqtt_client, topic):
  324. super().__init__(mqtt_client, topic)
  325. #
  326. # RX
  327. #
  328. @property
  329. def output_0(self):
  330. """rv: [True, False]"""
  331. return self.get(self.KEY_OUTPUT_0)
  332. @property
  333. def linkquality(self):
  334. """rv: numeric value"""
  335. return self.get(self.KEY_LINKQUALITY)
  336. #
  337. # TX
  338. #
  339. def set_output_0(self, state):
  340. """state: [True, False, 'toggle']"""
  341. self.pack(self.KEY_OUTPUT_0, state)
  342. def set_output_0_mcb(self, device, key, data):
  343. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  344. self.set_output_0(data)
  345. def toggle_output_0_mcb(self, device, key, data):
  346. self.logger.info("Toggeling output 0")
  347. self.set_output_0('toggle')
  348. def all_off(self):
  349. self.set_output_0(False)
  350. class silvercrest_motion_sensor(base):
  351. KEY_BATTERY = "battery"
  352. KEY_BATTERY_LOW = "battery_low"
  353. KEY_LINKQUALITY = "linkquality"
  354. KEY_OCCUPANCY = "occupancy"
  355. KEY_UNMOUNTED = "tamper"
  356. KEY_VOLTAGE = "voltage"
  357. #
  358. RX_KEYS = [KEY_BATTERY, KEY_BATTERY_LOW, KEY_LINKQUALITY, KEY_OCCUPANCY, KEY_UNMOUNTED, KEY_VOLTAGE]
  359. def __init__(self, mqtt_client, topic):
  360. super().__init__(mqtt_client, topic)
  361. def warning_call_condition(self):
  362. return self.get(self.KEY_BATTERY_LOW)
  363. def warning_text(self, data):
  364. return "Battery low: level=%d" % self.get(self.KEY_BATTERY)
  365. #
  366. # RX
  367. #
  368. @property
  369. def linkquality(self):
  370. """rv: numeric value"""
  371. return self.get(self.KEY_LINKQUALITY)
  372. class my_powerplug(base):
  373. KEY_OUTPUT_0 = "output/1"
  374. KEY_OUTPUT_1 = "output/2"
  375. KEY_OUTPUT_2 = "output/3"
  376. KEY_OUTPUT_3 = "output/4"
  377. KEY_OUTPUT_ALL = "output/all"
  378. KEY_OUTPUT_LIST = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  379. #
  380. TX_TYPE = base.TX_VALUE
  381. #
  382. RX_KEYS = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3]
  383. def __init__(self, mqtt_client, topic):
  384. super().__init__(mqtt_client, topic)
  385. #
  386. # RX
  387. #
  388. @property
  389. def output_0(self):
  390. """rv: [True, False]"""
  391. return self.get(self.KEY_OUTPUT_0)
  392. @property
  393. def output_1(self):
  394. """rv: [True, False]"""
  395. return self.get(self.KEY_OUTPUT_1)
  396. @property
  397. def output_2(self):
  398. """rv: [True, False]"""
  399. return self.get(self.KEY_OUTPUT_2)
  400. @property
  401. def output_3(self):
  402. """rv: [True, False]"""
  403. return self.get(self.KEY_OUTPUT_3)
  404. #
  405. # TX
  406. #
  407. def set_output(self, key, state):
  408. if key in self.KEY_OUTPUT_LIST:
  409. self.pack(key, state)
  410. else:
  411. logging.error("Unknown key to set the output!")
  412. def set_output_0(self, state):
  413. """state: [True, False, 'toggle']"""
  414. self.pack(self.KEY_OUTPUT_0, state)
  415. def set_output_0_mcb(self, device, key, data):
  416. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  417. self.set_output_0(data)
  418. def toggle_output_0_mcb(self, device, key, data):
  419. self.logger.info("Toggeling output 0")
  420. self.set_output_0('toggle')
  421. def set_output_1(self, state):
  422. """state: [True, False, 'toggle']"""
  423. self.pack(self.KEY_OUTPUT_1, state)
  424. def set_output_1_mcb(self, device, key, data):
  425. self.logger.log(logging.INFO if data != self.output_1 else logging.DEBUG, "Changing output 1 to %s", str(data))
  426. self.set_output_1(data)
  427. def toggle_output_1_mcb(self, device, key, data):
  428. self.logger.info("Toggeling output 1")
  429. self.set_output_1('toggle')
  430. def set_output_2(self, state):
  431. """state: [True, False, 'toggle']"""
  432. self.pack(self.KEY_OUTPUT_2, state)
  433. def set_output_2_mcb(self, device, key, data):
  434. self.logger.log(logging.INFO if data != self.output_2 else logging.DEBUG, "Changing output 2 to %s", str(data))
  435. self.set_output_2(data)
  436. def toggle_output_2_mcb(self, device, key, data):
  437. self.logger.info("Toggeling output 2")
  438. self.set_output_2('toggle')
  439. def set_output_3(self, state):
  440. """state: [True, False, 'toggle']"""
  441. self.pack(self.KEY_OUTPUT_3, state)
  442. def set_output_3_mcb(self, device, key, data):
  443. self.logger.log(logging.INFO if data != self.output_3 else logging.DEBUG, "Changing output 3 to %s", str(data))
  444. self.set_output_3(data)
  445. def toggle_output_3_mcb(self, device, key, data):
  446. self.logger.info("Toggeling output 3")
  447. self.set_output_3('toggle')
  448. def set_output_all(self, state):
  449. """state: [True, False, 'toggle']"""
  450. self.pack(self.KEY_OUTPUT_ALL, state)
  451. def set_output_all_mcb(self, device, key, data):
  452. self.logger.info("Changing all outputs to %s", str(data))
  453. self.set_output_all(data)
  454. def toggle_output_all_mcb(self, device, key, data):
  455. self.logger.info("Toggeling all outputs")
  456. self.set_output_0('toggle')
  457. def all_off(self):
  458. self.set_output_all(False)
  459. class tradfri_light(base):
  460. KEY_LINKQUALITY = "linkquality"
  461. KEY_OUTPUT_0 = "state"
  462. KEY_BRIGHTNESS = "brightness"
  463. KEY_COLOR_TEMP = "color_temp"
  464. KEY_BRIGHTNESS_FADE = "brightness_move"
  465. #
  466. TX_TYPE = base.TX_DICT
  467. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP, KEY_BRIGHTNESS_FADE]
  468. #
  469. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  470. RX_IGNORE_KEYS = ['update', 'color_mode', 'color_temp_startup']
  471. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  472. def __init__(self, mqtt_client, topic):
  473. super().__init__(mqtt_client, topic)
  474. def unpack_filter(self, key):
  475. if key == self.KEY_BRIGHTNESS:
  476. self[key] = (self[key] - 1) * 100 / 254
  477. elif key == self.KEY_COLOR_TEMP:
  478. self[key] = (self[key] - 250) * 10 / 204
  479. else:
  480. super().unpack_filter(key)
  481. def pack_filter(self, key, data):
  482. if key == self.KEY_BRIGHTNESS:
  483. return data * 254 / 100 + 1
  484. elif key == self.KEY_COLOR_TEMP:
  485. return data * 204 / 10 + 250
  486. else:
  487. return super().pack_filter(key, data)
  488. #
  489. # RX
  490. #
  491. @property
  492. def output_0(self):
  493. """rv: [True, False]"""
  494. return self.get(self.KEY_OUTPUT_0, False)
  495. @property
  496. def linkquality(self):
  497. """rv: numeric value"""
  498. return self.get(self.KEY_LINKQUALITY, 0)
  499. @property
  500. def brightness(self):
  501. """rv: numeric value [0%, ..., 100%]"""
  502. return self.get(self.KEY_BRIGHTNESS, 0)
  503. @property
  504. def color_temp(self):
  505. """rv: numeric value [0, ..., 10]"""
  506. return self.get(self.KEY_COLOR_TEMP, 0)
  507. #
  508. # TX
  509. #
  510. def request_data(self, device=None, key=None, data=None):
  511. self.mqtt_client.send(self.topic + "/get", '{"%s": ""}' % self.KEY_OUTPUT_0)
  512. def set_output_0(self, state):
  513. """state: [True, False, 'toggle']"""
  514. self.pack(self.KEY_OUTPUT_0, state)
  515. def set_output_0_mcb(self, device, key, data):
  516. self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
  517. self.set_output_0(data)
  518. def toggle_output_0_mcb(self, device, key, data):
  519. self.logger.info("Toggeling output 0")
  520. self.set_output_0('toggle')
  521. def set_brightness(self, brightness):
  522. """brightness: [0, ..., 100]"""
  523. self.pack(self.KEY_BRIGHTNESS, brightness)
  524. def set_brightness_mcb(self, device, key, data):
  525. self.logger.log(logging.INFO if data != self.brightness else logging.DEBUG, "Changing brightness to %s", str(data))
  526. self.set_brightness(data)
  527. def default_inc(self, speed=40):
  528. self.pack(self.KEY_BRIGHTNESS_FADE, speed)
  529. def default_dec(self, speed=-40):
  530. self.default_inc(speed)
  531. def default_stop(self):
  532. self.default_inc(0)
  533. def set_color_temp(self, color_temp):
  534. """color_temp: [0, ..., 10]"""
  535. self.pack(self.KEY_COLOR_TEMP, color_temp)
  536. def set_color_temp_mcb(self, device, key, data):
  537. self.logger.log(logging.INFO if data != self.color_temp else logging.DEBUG, "Changing color temperature to %s", str(data))
  538. self.set_color_temp(data)
  539. def all_off(self):
  540. self.set_output_0(False)
  541. class tradfri_button(base):
  542. ACTION_TOGGLE = "toggle"
  543. ACTION_BRIGHTNESS_UP = "brightness_up_click"
  544. ACTION_BRIGHTNESS_DOWN = "brightness_down_click"
  545. ACTION_RIGHT = "arrow_right_click"
  546. ACTION_LEFT = "arrow_left_click"
  547. ACTION_BRIGHTNESS_UP_LONG = "brightness_up_hold"
  548. ACTION_BRIGHTNESS_UP_RELEASE = "brightness_up_release"
  549. ACTION_BRIGHTNESS_DOWN_LONG = "brightness_down_hold"
  550. ACTION_BRIGHTNESS_DOWN_RELEASE = "brightness_down_release"
  551. ACTION_RIGHT_LONG = "arrow_right_hold"
  552. ACTION_RIGHT_RELEASE = "arrow_right_release"
  553. ACTION_LEFT_LONG = "arrow_left_hold"
  554. ACTION_LEFT_RELEASE = "arrow_left_release"
  555. #
  556. KEY_LINKQUALITY = "linkquality"
  557. KEY_BATTERY = "battery"
  558. KEY_ACTION = "action"
  559. KEY_ACTION_DURATION = "action_duration"
  560. #
  561. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_ACTION]
  562. RX_IGNORE_KEYS = ['update', KEY_ACTION_DURATION]
  563. def __init__(self, mqtt_client, topic):
  564. super().__init__(mqtt_client, topic)
  565. #
  566. # RX
  567. #
  568. @property
  569. def action(self):
  570. """rv: action_txt"""
  571. return self.get(self.KEY_ACTION)
  572. #
  573. # WARNING CALL
  574. #
  575. def warning_call_condition(self):
  576. return self.get(self.KEY_BATTERY) is not None and self.get(self.KEY_BATTERY) <= BATTERY_WARN_LEVEL
  577. def warning_text(self):
  578. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  579. class nodered_gui_leds(base):
  580. KEY_LED_0 = "led0"
  581. KEY_LED_1 = "led1"
  582. KEY_LED_2 = "led2"
  583. KEY_LED_3 = "led3"
  584. KEY_LED_4 = "led4"
  585. KEY_LED_5 = "led5"
  586. KEY_LED_6 = "led6"
  587. KEY_LED_7 = "led7"
  588. KEY_LED_8 = "led8"
  589. KEY_LED_9 = "led9"
  590. 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]
  591. #
  592. TX_TYPE = base.TX_VALUE
  593. def set_led(self, key, data):
  594. """data: [True, False]"""
  595. self.logger.debug("Sending %s with content %s", key, str(data))
  596. self.pack(key, data)
  597. class nodered_gui_timer(base):
  598. KEY_TIMER = "timer"
  599. #
  600. TX_TYPE = base.TX_VALUE
  601. def set_timer(self, data):
  602. """data: numeric"""
  603. self.pack(self.KEY_TIMER, data)
  604. def set_timer_mcb(self, device, key, data):
  605. self.logger.debug("Sending %s with content %s", key, str(data))
  606. self.set_timer(data)
  607. class nodered_gui_button(base):
  608. KEY_STATE = "state"
  609. #
  610. RX_KEYS = [KEY_STATE]
  611. #
  612. # RX
  613. #
  614. @property
  615. def state(self):
  616. """rv: [True, False]"""
  617. return self.get(self.KEY_STATE)
  618. class nodered_gui_switch(nodered_gui_button):
  619. TX_TYPE = base.TX_VALUE
  620. #
  621. # TX
  622. #
  623. def set_state(self, data):
  624. """data: [True, False]"""
  625. self.pack(self.KEY_STATE, data)
  626. def set_state_mcb(self, device, key, data):
  627. self.logger.debug("Sending %s with content %s", key, str(data))
  628. self.set_state(data)
  629. class nodered_gui_light(nodered_gui_switch, nodered_gui_leds, nodered_gui_timer):
  630. KEY_ENABLE = "enable"
  631. KEY_BRIGHTNESS = "brightness"
  632. KEY_COLOR_TEMP = "color_temp"
  633. #
  634. TX_TYPE = base.TX_VALUE
  635. #
  636. RX_KEYS = nodered_gui_switch.RX_KEYS + [KEY_ENABLE, KEY_BRIGHTNESS, KEY_COLOR_TEMP]
  637. #
  638. # RX
  639. #
  640. @property
  641. def enable(self):
  642. """rv: [True, False]"""
  643. return self.get(self.KEY_ENABLE)
  644. @property
  645. def brightness(self):
  646. """rv: [True, False]"""
  647. return self.get(self.KEY_BRIGHTNESS)
  648. @property
  649. def color_temp(self):
  650. """rv: [True, False]"""
  651. return self.get(self.KEY_COLOR_TEMP)
  652. #
  653. # TX
  654. #
  655. def set_enable(self, data):
  656. """data: [True, False]"""
  657. self.pack(self.KEY_ENABLE, data)
  658. def set_enable_mcb(self, device, key, data):
  659. self.logger.debug("Sending %s with content %s", key, str(data))
  660. self.set_enable(data)
  661. def set_brightness(self, data):
  662. """data: [0%, ..., 100%]"""
  663. self.pack(self.KEY_BRIGHTNESS, data)
  664. def set_brightness_mcb(self, device, key, data):
  665. self.logger.debug("Sending %s with content %s", key, str(data))
  666. self.set_brightness(data)
  667. def set_color_temp(self, data):
  668. """data: [0, ..., 10]"""
  669. self.pack(self.KEY_COLOR_TEMP, data)
  670. def set_color_temp_mcb(self, device, key, data):
  671. self.logger.debug("Sending %s with content %s", key, str(data))
  672. self.set_color_temp(data)
  673. class nodered_gui_radiator(nodered_gui_timer):
  674. KEY_TEMPERATURE = "temperature"
  675. KEY_SETPOINT_TEMP = "setpoint_temp"
  676. KEY_SETPOINT_TO_DEFAULT = "setpoint_to_default"
  677. KEY_BOOST = 'boost'
  678. KEY_AWAY = "away"
  679. KEY_SUMMER = "summer"
  680. KEY_ENABLE = "enable"
  681. #
  682. RX_KEYS = [KEY_TEMPERATURE, KEY_SETPOINT_TEMP, KEY_SETPOINT_TO_DEFAULT, KEY_BOOST, KEY_AWAY, KEY_SUMMER]
  683. #
  684. # TX
  685. #
  686. def set_temperature(self, data):
  687. """data: [True, False]"""
  688. self.pack(self.KEY_TEMPERATURE, data)
  689. def set_temperature_mcb(self, device, key, data):
  690. self.logger.debug("Sending %s with content %s", key, str(data))
  691. self.set_temperature(data)
  692. def set_setpoint_temperature(self, data):
  693. """data: [True, False]"""
  694. self.pack(self.KEY_SETPOINT_TEMP, data)
  695. def set_setpoint_temperature_mcb(self, device, key, data):
  696. self.logger.debug("Sending %s with content %s", key, str(data))
  697. self.set_setpoint_temperature(data)
  698. def set_away(self, data):
  699. """data: [True, False]"""
  700. self.pack(self.KEY_AWAY, data)
  701. def set_away_mcb(self, device, key, data):
  702. self.logger.debug("Sending %s with content %s", key, str(data))
  703. self.set_away(data)
  704. def set_summer(self, data):
  705. """data: [True, False]"""
  706. self.pack(self.KEY_SUMMER, data)
  707. def set_summer_mcb(self, device, key, data):
  708. self.logger.debug("Sending %s with content %s", key, str(data))
  709. self.set_summer(data)
  710. def set_enable(self, data):
  711. """data: [True, False]"""
  712. self.pack(self.KEY_ENABLE, data)
  713. def set_enable_mcb(self, device, key, data):
  714. self.logger.debug("Sending %s with content %s", key, str(data))
  715. self.set_enable(data)
  716. class brennenstuhl_heatingvalve(base):
  717. KEY_LINKQUALITY = "linkquality"
  718. KEY_BATTERY = "battery"
  719. KEY_HEATING_SETPOINT = "current_heating_setpoint"
  720. KEY_TEMPERATURE = "local_temperature"
  721. #
  722. KEY_AWAY_MODE = "away_mode"
  723. KEY_CHILD_LOCK = "child_lock"
  724. KEY_PRESET = "preset"
  725. KEY_SYSTEM_MODE = "system_mode"
  726. KEY_VALVE_DETECTION = "valve_detection"
  727. KEY_WINDOW_DETECTION = "window_detection"
  728. #
  729. TX_TYPE = base.TX_DICT
  730. #
  731. RX_KEYS = [KEY_LINKQUALITY, KEY_BATTERY, KEY_HEATING_SETPOINT, KEY_TEMPERATURE]
  732. RX_IGNORE_KEYS = [KEY_AWAY_MODE, KEY_CHILD_LOCK, KEY_PRESET, KEY_SYSTEM_MODE, KEY_VALVE_DETECTION, KEY_WINDOW_DETECTION]
  733. def __init__(self, mqtt_client, topic):
  734. super().__init__(mqtt_client, topic)
  735. self.mqtt_client.send(self.topic + '/' + self.TX_TOPIC, json.dumps({self.KEY_WINDOW_DETECTION: "ON",
  736. self.KEY_CHILD_LOCK: "UNLOCK", self.KEY_VALVE_DETECTION: "ON", self.KEY_SYSTEM_MODE: "heat", self.KEY_PRESET: "manual"}))
  737. def warning_call_condition(self):
  738. return self.get(self.KEY_BATTERY, 100) <= BATTERY_WARN_LEVEL
  739. def warning_text(self):
  740. return "Low battery level detected for %s. Battery level was %.0f%%." % (self.topic, self.get(self.KEY_BATTERY))
  741. #
  742. # RX
  743. #
  744. @property
  745. def linkqulity(self):
  746. return self.get(self.KEY_LINKQUALITY)
  747. @property
  748. def heating_setpoint(self):
  749. return self.get(self.KEY_HEATING_SETPOINT)
  750. @property
  751. def temperature(self):
  752. return self.get(self.KEY_TEMPERATURE)
  753. #
  754. # TX
  755. #
  756. def set_heating_setpoint(self, setpoint):
  757. self.pack(self.KEY_HEATING_SETPOINT, setpoint)
  758. def set_heating_setpoint_mcb(self, device, key, data):
  759. self.logger.info("Changing heating setpoint to %s", str(data))
  760. self.set_heating_setpoint(data)
  761. class remote(base):
  762. KEY_CD = "CD"
  763. KEY_LINE1 = "LINE1"
  764. KEY_LINE3 = "LINE3"
  765. KEY_MUTE = "MUTE"
  766. KEY_POWER = "POWER"
  767. KEY_VOLDOWN = "VOLDOWN"
  768. KEY_VOLUP = "VOLUP"
  769. #
  770. TX_TOPIC = ''
  771. TX_TYPE = base.TX_VALUE
  772. #
  773. RX_IGNORE_TOPICS = [KEY_CD, KEY_LINE1, KEY_LINE3, KEY_MUTE, KEY_POWER, KEY_VOLUP, KEY_VOLDOWN]
  774. def set_cd(self, device=None, key=None, data=None):
  775. self.pack(self.KEY_CD, None)
  776. def set_line1(self, device=None, key=None, data=None):
  777. self.pack(self.KEY_LINE1, None)
  778. def set_line3(self, device=None, key=None, data=None):
  779. self.pack(self.KEY_LINE3, None)
  780. def set_mute(self, device=None, key=None, data=None):
  781. self.pack(self.KEY_MUTE, None)
  782. def set_power(self, device=None, key=None, data=None):
  783. self.pack(self.KEY_POWER, None)
  784. def set_volume_up(self, data=False):
  785. """data: [True, False]"""
  786. self.pack(self.KEY_VOLUP, data)
  787. def set_volume_down(self, data=False):
  788. """data: [True, False]"""
  789. self.pack(self.KEY_VOLDOWN, data)
  790. def default_inc(self, device=None, key=None, data=None):
  791. self.set_volume_up(True)
  792. def default_dec(self, device=None, key=None, data=None):
  793. self.set_volume_down(True)
  794. def default_stop(self, device=None, key=None, data=None):
  795. self.set_volume_up(False)
  796. class status(base):
  797. KEY_STATE = "state"
  798. #
  799. TX_TYPE = base.TX_VALUE
  800. #
  801. RX_KEYS = [KEY_STATE]
  802. def set_state(self, num, data):
  803. """data: [True, False]"""
  804. self.pack(self.KEY_STATE + "/" + str(num), data)
  805. def set_state_mcb(self, device, key, data):
  806. self.logger.info("Changing state to %s", str(data))
  807. self.set_state(data)
  808. class audio_status(status):
  809. KEY_TITLE = "title"
  810. #
  811. RX_KEYS = [status.KEY_STATE, KEY_TITLE]