Smarthome Functionen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

__init__.py 29KB

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