Smarthome Functionen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

__init__.py 29KB

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