device_config.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. """
  2. Config parser for Tuya Local devices.
  3. """
  4. import logging
  5. from base64 import b64decode, b64encode
  6. from collections.abc import Sequence
  7. from datetime import datetime
  8. from fnmatch import fnmatch
  9. from numbers import Number
  10. from os import scandir
  11. from os.path import dirname, exists, join, splitext
  12. from homeassistant.util import slugify
  13. from homeassistant.util.yaml import load_yaml
  14. import custom_components.tuya_local.devices as config_dir
  15. _LOGGER = logging.getLogger(__name__)
  16. def _typematch(vtype, value):
  17. # Workaround annoying legacy of bool being a subclass of int in Python
  18. if vtype is int and isinstance(value, bool):
  19. return False
  20. # Allow integers to pass as floats.
  21. if vtype is float and isinstance(value, Number):
  22. return True
  23. if isinstance(value, vtype):
  24. return True
  25. # Allow values embedded in strings if they can be converted
  26. # But not for bool, as everything can be converted to bool
  27. elif isinstance(value, str) and vtype is not bool:
  28. try:
  29. vtype(value)
  30. return True
  31. except ValueError:
  32. return False
  33. return False
  34. def _scale_range(r, s):
  35. "Scale range r by factor s"
  36. return (r["min"] / s, r["max"] / s)
  37. _unsigned_fmts = {
  38. 1: "B",
  39. 2: "H",
  40. 3: "3s",
  41. 4: "I",
  42. }
  43. _signed_fmts = {
  44. 1: "b",
  45. 2: "h",
  46. 3: "3s",
  47. 4: "i",
  48. }
  49. def _bytes_to_fmt(bytes, signed=False):
  50. """Convert a byte count to an unpack format."""
  51. fmt = _signed_fmts if signed else _unsigned_fmts
  52. if bytes in fmt:
  53. return fmt[bytes]
  54. else:
  55. return f"{bytes}s"
  56. def _equal_or_in(value1, values2):
  57. """Return true if value1 is the same as values2, or appears in values2."""
  58. if not isinstance(values2, str) and isinstance(values2, Sequence):
  59. return value1 in values2
  60. else:
  61. return value1 == values2
  62. def _remove_duplicates(seq):
  63. """Remove dulicates from seq, maintaining order."""
  64. if not seq:
  65. return []
  66. seen = set()
  67. adder = seen.add
  68. return [x for x in seq if not (x in seen or adder(x))]
  69. def to_signed(val, bits):
  70. """Convert unsigned int to signed 2's complement of given bit length."""
  71. if val & (1 << (bits - 1)):
  72. return val - (1 << bits)
  73. return val
  74. class TuyaDeviceConfig:
  75. """Representation of a device config for Tuya Local devices."""
  76. def __init__(self, fname):
  77. """Initialize the device config.
  78. Args:
  79. fname (string): The filename of the yaml config to load."""
  80. _CONFIG_DIR = dirname(config_dir.__file__)
  81. self._fname = fname
  82. filename = join(_CONFIG_DIR, fname)
  83. self._config = load_yaml(filename)
  84. self._reported_deprecated_primary = False
  85. _LOGGER.debug("Loaded device config %s", fname)
  86. @property
  87. def name(self):
  88. """Return the friendly name for this device."""
  89. return self._config["name"]
  90. @property
  91. def config(self):
  92. """Return the config file associated with this device."""
  93. return self._fname
  94. @property
  95. def config_type(self):
  96. """Return the config type associated with this device."""
  97. return splitext(self._fname)[0]
  98. @property
  99. def legacy_type(self):
  100. """Return the legacy conf_type associated with this device."""
  101. return self._config.get("legacy_type", self.config_type)
  102. @property
  103. def primary_entity(self):
  104. """Return the primary type of entity for this device."""
  105. if "primary_entity" not in self._config:
  106. # primary entity is a deprecated fallback, so if it is
  107. # missing, we need to log a warning about the missing entities
  108. # list.
  109. _LOGGER.error(f"{self.config_type}.yaml does not define an entities list.")
  110. return TuyaEntityConfig(self, self._config["entities"][0])
  111. if not self._reported_deprecated_primary:
  112. _LOGGER.warning(
  113. f"{self.config_type}.yaml distinguishes between primary"
  114. " and secondary_entities. This is deprecated, please"
  115. " modify it to use a single list."
  116. )
  117. self._reported_deprecated_primary = True
  118. return TuyaEntityConfig(self, self._config["primary_entity"])
  119. def all_entities(self):
  120. """Iterate through all entities for this device."""
  121. entities = self._config.get("entities")
  122. if not entities:
  123. yield self.primary_entity
  124. entities = self._config.get("secondary_entities", {})
  125. for e in entities:
  126. yield TuyaEntityConfig(self, e)
  127. def matches(self, dps, product_ids):
  128. """Determine whether this config matches the provided dps map or
  129. product ids."""
  130. product_match = False
  131. if product_ids:
  132. for p in self._config.get("products", []):
  133. if p.get("id", "MISSING_ID!?!") in product_ids:
  134. product_match = True
  135. required_dps = self._get_required_dps()
  136. missing_dps = [dp for dp in required_dps if dp.id not in dps.keys()]
  137. if len(missing_dps) > 0:
  138. _LOGGER.debug(
  139. "Not match for %s, missing required DPs: %s",
  140. self.name,
  141. [{dp.id: dp.type.__name__} for dp in missing_dps],
  142. )
  143. incorrect_type_dps = [
  144. dp
  145. for dp in self._get_all_dps()
  146. if dp.id in dps.keys() and not _typematch(dp.type, dps[dp.id])
  147. ]
  148. if len(incorrect_type_dps) > 0:
  149. _LOGGER.debug(
  150. "Not match for %s, DPs have incorrect type: %s",
  151. self.name,
  152. [{dp.id: dp.type.__name__} for dp in incorrect_type_dps],
  153. )
  154. if product_match:
  155. _LOGGER.warning(
  156. "Product matches %s but dps mismatched",
  157. self.name,
  158. )
  159. return False
  160. return product_match or len(missing_dps) == 0
  161. def _get_all_dps(self):
  162. all_dps_list = []
  163. all_dps_list += [d for dev in self.all_entities() for d in dev.dps()]
  164. return all_dps_list
  165. def _get_required_dps(self):
  166. required_dps_list = [d for d in self._get_all_dps() if not d.optional]
  167. return required_dps_list
  168. def _entity_match_analyse(self, entity, keys, matched, dps, product_match):
  169. """
  170. Determine whether this entity can be a match for the dps
  171. Args:
  172. entity - the TuyaEntityConfig to check against
  173. keys - the unmatched keys for the device
  174. matched - the matched keys for the device
  175. dps - the dps values to be matched
  176. Side Effects:
  177. Moves items from keys to matched if they match dps
  178. Return Value:
  179. True if all dps in entity could be matched to dps, False otherwise
  180. """
  181. all_dp = keys + matched
  182. for d in entity.dps():
  183. if (d.id not in all_dp and not d.optional and not product_match) or (
  184. d.id in all_dp and not _typematch(d.type, dps[d.id])
  185. ):
  186. return False
  187. if d.id in keys:
  188. matched.append(d.id)
  189. keys.remove(d.id)
  190. return True
  191. def match_quality(self, dps, product_ids=None):
  192. """Determine the match quality for the provided dps map and product ids."""
  193. product_match = 0
  194. if product_ids:
  195. for p in self._config.get("products", []):
  196. if p.get("id", "MISSING_ID!?!") in product_ids:
  197. product_match = 101
  198. keys = list(dps.keys())
  199. matched = []
  200. if "updated_at" in keys:
  201. keys.remove("updated_at")
  202. total = len(keys)
  203. if total < 1:
  204. return product_match
  205. for e in self.all_entities():
  206. if not self._entity_match_analyse(e, keys, matched, dps, product_match > 0):
  207. return 0
  208. return product_match or round((total - len(keys)) * 100 / total)
  209. class TuyaEntityConfig:
  210. """Representation of an entity config for a supported entity."""
  211. def __init__(self, device, config):
  212. self._device = device
  213. self._config = config
  214. @property
  215. def name(self):
  216. """The friendly name for this entity."""
  217. return self._config.get("name")
  218. @property
  219. def translation_key(self):
  220. """The translation key for this entity."""
  221. return self._config.get("translation_key")
  222. @property
  223. def translation_only_key(self):
  224. """The translation key for this entity, not used for unique_id"""
  225. return self._config.get("translation_only_key")
  226. @property
  227. def translation_placeholders(self):
  228. """The translation placeholders for this entity."""
  229. return self._config.get("translation_placeholders", {})
  230. def unique_id(self, device_uid):
  231. """Return a suitable unique_id for this entity."""
  232. return f"{device_uid}-{slugify(self.config_id)}"
  233. @property
  234. def entity_category(self):
  235. return self._config.get("category")
  236. @property
  237. def deprecated(self):
  238. """Return whether this entity is deprecated."""
  239. return "deprecated" in self._config.keys()
  240. @property
  241. def deprecation_message(self):
  242. """Return a deprecation message for this entity"""
  243. replacement = self._config.get(
  244. "deprecated", "nothing, this warning has been raised in error"
  245. )
  246. return (
  247. f"The use of {self.config_id} for {self._device.name} is "
  248. f"deprecated and should be replaced by {replacement}."
  249. )
  250. @property
  251. def entity(self):
  252. """The entity type of this entity."""
  253. return self._config["entity"]
  254. @property
  255. def config_id(self):
  256. """The identifier for this entity in the config."""
  257. own_name = self._config.get("name")
  258. if own_name:
  259. return f"{self.entity}_{slugify(own_name)}"
  260. if self.translation_key:
  261. slug = f"{self.entity}_{self.translation_key}"
  262. for key, value in self.translation_placeholders.items():
  263. if key in slug:
  264. slug = slug.replace(key, slugify(value))
  265. else:
  266. slug = f"{slug}_{value}"
  267. return slug
  268. elif self.device_class:
  269. return f"{self.entity}_{self.device_class}"
  270. return self.entity
  271. @property
  272. def device_class(self):
  273. """The device class of this entity."""
  274. return self._config.get("class")
  275. def icon(self, device):
  276. """Return the icon for this entity, with state as given."""
  277. icon = self._config.get("icon", None)
  278. priority = self._config.get("icon_priority", 100)
  279. for d in self.dps():
  280. rule = d.icon_rule(device)
  281. if rule and rule["priority"] < priority:
  282. icon = rule["icon"]
  283. priority = rule["priority"]
  284. return icon
  285. @property
  286. def mode(self):
  287. """Return the mode (used by Number entities)."""
  288. return self._config.get("mode")
  289. def dps(self):
  290. """Iterate through the list of dps for this entity."""
  291. for d in self._config["dps"]:
  292. yield TuyaDpsConfig(self, d)
  293. def find_dps(self, name):
  294. """Find a dps with the specified name."""
  295. for d in self.dps():
  296. if d.name == name:
  297. return d
  298. return None
  299. def available(self, device):
  300. """Return whether this entity should be available, with state as given."""
  301. avail_dp = self.find_dps("available")
  302. if avail_dp and device.has_returned_state:
  303. return avail_dp.get_value(device)
  304. return device.has_returned_state
  305. def enabled_by_default(self, device):
  306. """Return whether this entity should be disabled by default."""
  307. hidden = self._config.get("hidden", False)
  308. if hidden == "unavailable":
  309. avail_dp = self.find_dps("available")
  310. if not avail_dp:
  311. _LOGGER.warning(
  312. "Entity %s / %s has hidden: unavailable but no available dp defined",
  313. self._device.config_type,
  314. self.name,
  315. )
  316. hidden = not self.available(device)
  317. return not hidden and not self.deprecated
  318. class TuyaDpsConfig:
  319. """Representation of a dps config."""
  320. def __init__(self, entity, config):
  321. self._entity = entity
  322. self._config = config
  323. self.stringify = False
  324. @property
  325. def id(self):
  326. return str(self._config["id"])
  327. @property
  328. def type(self):
  329. t = self._config["type"]
  330. types = {
  331. "boolean": bool,
  332. "integer": int,
  333. "string": str,
  334. "float": float,
  335. "bitfield": int,
  336. "json": str,
  337. "base64": str,
  338. "utf16b64": str,
  339. "hex": str,
  340. "unixtime": int,
  341. }
  342. return types.get(t)
  343. @property
  344. def rawtype(self):
  345. return self._config["type"]
  346. @property
  347. def name(self):
  348. return self._config["name"]
  349. @property
  350. def optional(self):
  351. return self._config.get("optional", False)
  352. @property
  353. def persist(self):
  354. return self._config.get("persist", True)
  355. @property
  356. def force(self):
  357. return self._config.get("force", False)
  358. @property
  359. def sensitive(self):
  360. return self._config.get("sensitive", False)
  361. @property
  362. def format(self):
  363. fmt = self._config.get("format")
  364. if fmt:
  365. unpack_fmt = ">"
  366. ranges = []
  367. names = []
  368. for f in fmt:
  369. name = f.get("name")
  370. b = f.get("bytes", 1)
  371. r = f.get("range")
  372. if r:
  373. mn = r.get("min")
  374. mx = r.get("max")
  375. else:
  376. mn = 0
  377. mx = 256**b - 1
  378. unpack_fmt = unpack_fmt + _bytes_to_fmt(b, mn < 0)
  379. ranges.append({"min": mn, "max": mx})
  380. names.append(name)
  381. _LOGGER.debug("format of %s found", unpack_fmt)
  382. return {"format": unpack_fmt, "ranges": ranges, "names": names}
  383. return None
  384. @property
  385. def mask(self):
  386. mask = self._config.get("mask")
  387. if mask:
  388. return int(mask, 16)
  389. @property
  390. def endianness(self):
  391. endianness = self._config.get("endianness", "big")
  392. return endianness
  393. def get_value(self, device):
  394. """Return the value of the dps from the given device."""
  395. mask = self.mask
  396. bytevalue = self.decoded_value(device)
  397. if mask and isinstance(bytevalue, bytes):
  398. value = int.from_bytes(bytevalue, self.endianness)
  399. scale = mask & (1 + ~mask)
  400. raw_result = (value & mask) // scale
  401. # Insert signed interpretation here
  402. if self._config.get("mask_signed", False):
  403. # Count how many bits are set in the mask
  404. bit_count = mask.bit_count()
  405. raw_result = to_signed(raw_result, bit_count)
  406. return self._map_from_dps(raw_result, device)
  407. else:
  408. return self._map_from_dps(device.get_property(self.id), device)
  409. def decoded_value(self, device):
  410. v = self._map_from_dps(device.get_property(self.id), device)
  411. return self.decode_value(v, device)
  412. def decode_value(self, v, device):
  413. if self.rawtype == "hex" and isinstance(v, str):
  414. try:
  415. return bytes.fromhex(v)
  416. except ValueError:
  417. _LOGGER.warning(
  418. "%s sent invalid hex '%s' for %s",
  419. device.name,
  420. v,
  421. self.name,
  422. )
  423. return None
  424. elif self.rawtype == "base64" and isinstance(v, str):
  425. try:
  426. return b64decode(v)
  427. except ValueError:
  428. _LOGGER.warning(
  429. "%s sent invalid base64 '%s' for %s",
  430. device.name,
  431. v,
  432. self.name,
  433. )
  434. return None
  435. else:
  436. return v
  437. def encode_value(self, v):
  438. if self.rawtype == "hex":
  439. return v.hex()
  440. elif self.rawtype == "base64":
  441. return b64encode(v).decode("utf-8")
  442. elif self.rawtype == "unixtime" and isinstance(v, datetime):
  443. return v.timestamp()
  444. else:
  445. return v
  446. def _match(self, matchdata, value):
  447. """Return true val1 matches val2"""
  448. if self.rawtype == "bitfield" and matchdata:
  449. try:
  450. return (int(value) & int(matchdata)) != 0
  451. except (TypeError, ValueError):
  452. return False
  453. else:
  454. return str(value) == str(matchdata)
  455. async def async_set_value(self, device, value):
  456. """Set the value of the dps in the given device to given value."""
  457. if self.readonly:
  458. raise TypeError(f"{self.name} is read only")
  459. if self.invalid_for(value, device):
  460. raise AttributeError(f"{self.name} cannot be set at this time")
  461. settings = self.get_values_to_set(device, value)
  462. await device.async_set_properties(settings)
  463. def mapping_available(self, mapping, device):
  464. """Determine if this mapping should be available."""
  465. if "available" in mapping:
  466. avail_dp = self._entity.find_dps(mapping.get("available"))
  467. if avail_dp:
  468. return avail_dp.get_value(device)
  469. return True
  470. def should_show_mapping(self, mapping, device):
  471. """Determine if this mapping should be shown in the list of values."""
  472. if "value" not in mapping or mapping.get("hidden", False):
  473. return False
  474. return self.mapping_available(mapping, device)
  475. def values(self, device):
  476. """Return the possible values a dps can take."""
  477. if "mapping" not in self._config.keys():
  478. return []
  479. val = []
  480. for m in self._config["mapping"]:
  481. if self.should_show_mapping(m, device):
  482. val.append(m["value"])
  483. # If there is mirroring without override, include mirrored values
  484. elif "value_mirror" in m:
  485. r_dps = self._entity.find_dps(m["value_mirror"])
  486. if r_dps:
  487. val = val + r_dps.values(device)
  488. for c in m.get("conditions", {}):
  489. if self.should_show_mapping(c, device):
  490. val.append(c["value"])
  491. elif "value_mirror" in c:
  492. r_dps = self._entity.find_dps(c["value_mirror"])
  493. if r_dps:
  494. val = val + r_dps.values(device)
  495. cond = self._active_condition(m, device)
  496. if cond and "mapping" in cond:
  497. c_val = []
  498. for m2 in cond["mapping"]:
  499. if self.should_show_mapping(m2, device):
  500. c_val.append(m2["value"])
  501. elif "value_mirror" in m:
  502. r_dps = self._entity.find_dps(m["value_mirror"])
  503. if r_dps:
  504. c_val = c_val + r_dps.values(device)
  505. # if given, the conditional mapping is an override
  506. if c_val:
  507. val = c_val
  508. break
  509. return _remove_duplicates(val)
  510. @property
  511. def default(self):
  512. """Return the default value for a dp."""
  513. if "mapping" not in self._config.keys():
  514. _LOGGER.debug(
  515. "No mapping for %s, unable to determine default value",
  516. self.name,
  517. )
  518. return None
  519. for m in self._config["mapping"]:
  520. if m.get("default", False):
  521. return m.get("value", m.get("dps_val", None))
  522. for c in m.get("conditions", {}):
  523. if c.get("default", False):
  524. return c.get("value", m.get("value", m.get("dps_val", None)))
  525. def range(self, device, scaled=True):
  526. """Return the range for this dps if configured."""
  527. scale = self.scale(device) if scaled else 1
  528. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  529. r = self._config.get("range")
  530. if mapping:
  531. r = mapping.get("range", r)
  532. cond = self._active_condition(mapping, device)
  533. if cond:
  534. r = cond.get("range", r)
  535. if r and "min" in r and "max" in r:
  536. return _scale_range(r, scale)
  537. else:
  538. return None
  539. def scale(self, device):
  540. scale = 1
  541. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  542. if mapping:
  543. scale = mapping.get("scale", 1)
  544. cond = self._active_condition(mapping, device)
  545. if cond:
  546. scale = cond.get("scale", scale)
  547. return scale
  548. def precision(self, device):
  549. if self.type is int:
  550. scale = self.scale(device)
  551. precision = 0
  552. while scale > 1.0:
  553. scale /= 10.0
  554. precision += 1
  555. return precision
  556. @property
  557. def suggested_display_precision(self):
  558. return self._config.get("precision")
  559. def step(self, device, scaled=True):
  560. step = 1
  561. scale = self.scale(device) if scaled else 1
  562. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  563. if mapping:
  564. step = mapping.get("step", 1)
  565. cond = self._active_condition(mapping, device)
  566. if cond:
  567. step = cond.get("step", step)
  568. if step != 1 or scale != 1:
  569. _LOGGER.debug(
  570. "Step for %s is %s with scale %s",
  571. self.name,
  572. step,
  573. scale,
  574. )
  575. return step / scale if scaled else step
  576. @property
  577. def readonly(self):
  578. return self._config.get("readonly", False)
  579. def invalid_for(self, value, device):
  580. mapping = self._find_map_for_value(value, device)
  581. if mapping:
  582. cond = self._active_condition(mapping, device)
  583. if cond:
  584. return cond.get("invalid", False)
  585. return False
  586. @property
  587. def hidden(self):
  588. return self._config.get("hidden", False)
  589. @property
  590. def unit(self):
  591. return self._config.get("unit")
  592. @property
  593. def state_class(self):
  594. """The state class of this measurement."""
  595. return self._config.get("class")
  596. def _find_map_for_dps(self, value, device):
  597. default = None
  598. for m in self._config.get("mapping", {}):
  599. if not self.mapping_available(m, device) and "conditions" not in m:
  600. continue
  601. if "dps_val" not in m:
  602. default = m
  603. elif self._match(m["dps_val"], value):
  604. return m
  605. return default
  606. def _correct_type(self, result):
  607. """Convert value to the correct type for this dp."""
  608. if self.type is int:
  609. _LOGGER.debug("Rounding %s", self.name)
  610. result = int(round(result))
  611. elif self.type is bool:
  612. result = True if result else False
  613. elif self.type is float:
  614. result = float(result)
  615. elif self.type is str:
  616. result = str(result)
  617. if self.rawtype == "utf16b64":
  618. result = b64encode(result.encode("utf-16-be")).decode("utf-8")
  619. if self.stringify:
  620. result = str(result)
  621. return result
  622. def _map_from_dps(self, val, device):
  623. if val is not None and self.type is not str and isinstance(val, str):
  624. try:
  625. val = self.type(val)
  626. self.stringify = True
  627. except ValueError:
  628. self.stringify = False
  629. else:
  630. self.stringify = False
  631. # decode utf-16 base64 strings first, so normal strings can be matched
  632. if self.rawtype == "utf16b64" and isinstance(val, str):
  633. try:
  634. val = b64decode(val).decode("utf-16-be")
  635. except ValueError:
  636. _LOGGER.warning("Invalid utf16b64 %s", val)
  637. result = val
  638. scale = self.scale(device)
  639. replaced = False
  640. mapping = self._find_map_for_dps(val, device)
  641. if mapping:
  642. invert = mapping.get("invert", False)
  643. redirect = mapping.get("value_redirect")
  644. mirror = mapping.get("value_mirror")
  645. replaced = "value" in mapping
  646. result = mapping.get("value", result)
  647. target_range = mapping.get("target_range")
  648. cond = self._active_condition(mapping, device)
  649. if cond:
  650. if cond.get("invalid", False):
  651. return None
  652. replaced = replaced or "value" in cond
  653. result = cond.get("value", result)
  654. redirect = cond.get("value_redirect", redirect)
  655. mirror = cond.get("value_mirror", mirror)
  656. target_range = cond.get("target_range", target_range)
  657. for m in cond.get("mapping", {}):
  658. if str(m.get("dps_val")) == str(result):
  659. replaced = "value" in m
  660. result = m.get("value", result)
  661. if redirect:
  662. _LOGGER.debug("Redirecting %s to %s", self.name, redirect)
  663. r_dps = self._entity.find_dps(redirect)
  664. if r_dps:
  665. return r_dps.get_value(device)
  666. if mirror:
  667. r_dps = self._entity.find_dps(mirror)
  668. if r_dps:
  669. return r_dps.get_value(device)
  670. if invert and isinstance(result, Number):
  671. r = self._config.get("range")
  672. if r and "min" in r and "max" in r:
  673. result = -1 * result + r["min"] + r["max"]
  674. replaced = True
  675. if target_range and isinstance(result, Number):
  676. r = self._config.get("range")
  677. if r and "max" in r and "max" in target_range:
  678. from_min = r.get("min", 0)
  679. from_max = r["max"]
  680. to_min = target_range.get("min", 0)
  681. to_max = target_range["max"]
  682. result = to_min + (
  683. (result - from_min) * (to_max - to_min) / (from_max - from_min)
  684. )
  685. replaced = True
  686. if scale != 1 and isinstance(result, Number):
  687. result = result / scale
  688. replaced = True
  689. if self.rawtype == "unixtime" and isinstance(result, int):
  690. try:
  691. result = datetime.fromtimestamp(result)
  692. replaced = True
  693. except Exception:
  694. _LOGGER.warning("Invalid timestamp %d", result)
  695. if replaced:
  696. _LOGGER.debug(
  697. "%s: Mapped dps %s value from %s to %s",
  698. self._entity._device.name,
  699. self.id,
  700. val,
  701. result,
  702. )
  703. return result
  704. def _find_map_for_value(self, value, device):
  705. default = None
  706. nearest = None
  707. distance = float("inf")
  708. for m in self._config.get("mapping", {}):
  709. # no reverse mapping of hidden values
  710. ignore = m.get("hidden", False) or not self.mapping_available(m, device)
  711. if "dps_val" not in m and not ignore:
  712. default = m
  713. # The following avoids further matching on the above case
  714. # and in the null mapping case, which is intended to be
  715. # a one-way map to prevent the entity showing as unavailable
  716. # when no value is being reported by the device.
  717. if m.get("dps_val") is None:
  718. ignore = True
  719. if "value" in m and str(m["value"]) == str(value) and not ignore:
  720. return m
  721. if (
  722. "value" in m
  723. and isinstance(m["value"], Number)
  724. and isinstance(value, Number)
  725. and not ignore
  726. ):
  727. d = abs(m["value"] - value)
  728. if d < distance:
  729. distance = d
  730. nearest = m
  731. if "value" not in m and "value_mirror" in m and not ignore:
  732. r_dps = self._entity.find_dps(m["value_mirror"])
  733. if r_dps and str(r_dps.get_value(device)) == str(value):
  734. return m
  735. for c in m.get("conditions", {}):
  736. if c.get("hidden", False) or not self.mapping_available(c, device):
  737. continue
  738. if "value" in c and str(c["value"]) == str(value):
  739. c_dp = self._entity.find_dps(m.get("constraint", self.name))
  740. # only consider the condition a match if we can change
  741. # the dp to match, or it already matches
  742. if (c_dp and c_dp.id != self.id and not c_dp.readonly) or (
  743. _equal_or_in(
  744. device.get_property(c_dp.id),
  745. c.get("dps_val"),
  746. )
  747. ):
  748. return m
  749. if "value" not in c and "value_mirror" in c:
  750. r_dps = self._entity.find_dps(c["value_mirror"])
  751. if r_dps and str(r_dps.get_value(device)) == str(value):
  752. return m
  753. if nearest:
  754. return nearest
  755. return default
  756. def _active_condition(self, mapping, device, value=None):
  757. constraint = mapping.get("constraint", self.name)
  758. conditions = mapping.get("conditions")
  759. c_match = None
  760. if constraint and conditions:
  761. c_dps = self._entity.find_dps(constraint)
  762. # base64 and hex have to be decoded
  763. c_val = (
  764. None
  765. if c_dps is None
  766. else (
  767. c_dps.get_value(device)
  768. if c_dps.rawtype == "base64" or c_dps.rawtype == "hex"
  769. else device.get_property(c_dps.id)
  770. )
  771. )
  772. for cond in conditions:
  773. if not self.mapping_available(cond, device):
  774. continue
  775. if c_val is not None and (_equal_or_in(c_val, cond.get("dps_val"))):
  776. c_match = cond
  777. # Case where matching None, need extra checks to ensure we
  778. # are not just defaulting and it is really a match
  779. elif (
  780. c_val is None
  781. and c_dps is not None
  782. and "dps_val" in cond
  783. and cond.get("dps_val") is None
  784. ):
  785. c_match = cond
  786. # when changing, another condition may become active
  787. # return that if it exists over a current condition
  788. if value is not None and value == cond.get("value"):
  789. return cond
  790. return c_match
  791. def get_values_to_set(self, device, value, pending_map={}):
  792. """Return the dps values that would be set when setting to value"""
  793. result = value
  794. dps_map = {}
  795. if self.readonly:
  796. return dps_map
  797. # Special case: if the current value has a redirect mapping,
  798. # follow that.
  799. current_value = device.get_property(self.id)
  800. current_mapping = self._find_map_for_dps(current_value, device)
  801. if current_mapping:
  802. redirect = current_mapping.get("value_redirect")
  803. if redirect:
  804. return self._entity.find_dps(redirect).get_values_to_set(
  805. device,
  806. value,
  807. )
  808. # If no redirect, we need to check for mapped values in reverse
  809. mapping = self._find_map_for_value(value, device)
  810. scale = self.scale(device)
  811. mask = self.mask
  812. if mapping:
  813. replaced = False
  814. redirect = mapping.get("value_redirect")
  815. invert = mapping.get("invert", False)
  816. target_range = mapping.get("target_range")
  817. step = mapping.get("step")
  818. if not isinstance(step, Number):
  819. step = None
  820. if "dps_val" in mapping:
  821. result = mapping["dps_val"]
  822. replaced = True
  823. # Conditions may have side effect of setting another value.
  824. cond = self._active_condition(mapping, device, value)
  825. if cond:
  826. cval = cond.get("value")
  827. if cval is None:
  828. r_dps = cond.get("value_mirror")
  829. if r_dps:
  830. mirror = self._entity.find_dps(r_dps)
  831. if mirror:
  832. cval = mirror.get_value(device)
  833. if cval == value:
  834. c_dps = self._entity.find_dps(mapping.get("constraint", self.name))
  835. cond_dpsval = cond.get("dps_val")
  836. single_match = isinstance(cond_dpsval, str) or (
  837. not isinstance(cond_dpsval, Sequence)
  838. )
  839. if c_dps and c_dps.id != self.id and single_match:
  840. c_val = c_dps._map_from_dps(
  841. cond.get("dps_val", device.get_property(c_dps.id)),
  842. device,
  843. )
  844. dps_map.update(
  845. c_dps.get_values_to_set(device, c_val, pending_map)
  846. )
  847. # Allow simple conditional mapping overrides
  848. for m in cond.get("mapping", {}):
  849. if m.get("value") == value and not m.get("hidden", False):
  850. result = m.get("dps_val", result)
  851. step = cond.get("step", step)
  852. redirect = cond.get("value_redirect", redirect)
  853. target_range = cond.get("target_range", target_range)
  854. if redirect:
  855. _LOGGER.debug("Redirecting %s to %s", self.name, redirect)
  856. r_dps = self._entity.find_dps(redirect)
  857. if r_dps:
  858. return r_dps.get_values_to_set(device, value)
  859. if scale != 1 and isinstance(result, Number):
  860. _LOGGER.debug("Scaling %s by %s", result, scale)
  861. result = result * scale
  862. remap = self._find_map_for_value(result, device)
  863. if (
  864. remap
  865. and "dps_val" in remap
  866. and "dps_val" not in mapping
  867. and not remap.get("hidden", False)
  868. ):
  869. result = remap["dps_val"]
  870. replaced = True
  871. if target_range and isinstance(result, Number):
  872. r = self._config.get("range")
  873. if r and "max" in r and "max" in target_range:
  874. from_min = target_range.get("min", 0)
  875. from_max = target_range["max"]
  876. to_min = r.get("min", 0)
  877. to_max = r["max"]
  878. result = to_min + (
  879. (result - from_min) * (to_max - to_min) / (from_max - from_min)
  880. )
  881. replaced = True
  882. if invert:
  883. r = self._config.get("range")
  884. if r and "min" in r and "max" in r:
  885. result = -1 * result + r["min"] + r["max"]
  886. replaced = True
  887. if step and isinstance(result, Number):
  888. _LOGGER.debug("Stepping %s to %s", result, step)
  889. result = step * round(float(result) / step)
  890. remap = self._find_map_for_value(result, device)
  891. if (
  892. remap
  893. and "dps_val" in remap
  894. and "dps_val" not in mapping
  895. and not remap.get("hidden", False)
  896. ):
  897. result = remap["dps_val"]
  898. replaced = True
  899. if replaced:
  900. _LOGGER.debug(
  901. "%s: Mapped dps %s to %s from %s",
  902. self._entity._device.name,
  903. self.id,
  904. result,
  905. value,
  906. )
  907. r = self.range(device, scaled=False)
  908. if r and isinstance(result, Number):
  909. mn = r[0]
  910. mx = r[1]
  911. if round(result) < mn or round(result) > mx:
  912. # Output scaled values in the error message
  913. r = self.range(device, scaled=True)
  914. mn = r[0]
  915. mx = r[1]
  916. raise ValueError(f"{self.name} ({value}) must be between {mn} and {mx}")
  917. if mask and isinstance(result, bool):
  918. result = int(result)
  919. if mask and isinstance(result, Number):
  920. # mask is in hex, 2 digits/characters per byte
  921. hex_mask = self._config.get("mask")
  922. length = int(len(hex_mask) / 2)
  923. # Convert to int
  924. endianness = self.endianness
  925. mask_scale = mask & (1 + ~mask)
  926. decoded_value = self.decoded_value(device)
  927. # if we have already updated it as part of this update,
  928. # use it to preserve bits
  929. if self.id in pending_map:
  930. decoded_value = self.decode_value(pending_map[self.id], device)
  931. current_value = int.from_bytes(decoded_value, endianness)
  932. result = (current_value & ~mask) | (mask & int(result * mask_scale))
  933. result = self.encode_value(result.to_bytes(length, endianness))
  934. dps_map[self.id] = self._correct_type(result)
  935. return dps_map
  936. def icon_rule(self, device):
  937. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  938. icon = None
  939. priority = 100
  940. if mapping:
  941. icon = mapping.get("icon", icon)
  942. priority = mapping.get("icon_priority", 10 if icon else 100)
  943. cond = self._active_condition(mapping, device)
  944. if cond and cond.get("icon_priority", 10) < priority:
  945. icon = cond.get("icon", icon)
  946. priority = cond.get("icon_priority", 10 if icon else 100)
  947. return {"priority": priority, "icon": icon}
  948. def available_configs():
  949. """List the available config files."""
  950. _CONFIG_DIR = dirname(config_dir.__file__)
  951. for direntry in scandir(_CONFIG_DIR):
  952. if direntry.is_file() and fnmatch(direntry.name, "*.yaml"):
  953. yield direntry.name
  954. def possible_matches(dps, product_ids=None):
  955. """Return possible matching configs for a given set of
  956. dps values and product_ids."""
  957. for cfg in available_configs():
  958. parsed = TuyaDeviceConfig(cfg)
  959. try:
  960. if parsed.matches(dps, product_ids):
  961. yield parsed
  962. except TypeError:
  963. _LOGGER.error("Parse error in %s", cfg)
  964. def get_config(conf_type):
  965. """
  966. Return a config to use with config_type.
  967. """
  968. _CONFIG_DIR = dirname(config_dir.__file__)
  969. fname = conf_type + ".yaml"
  970. fpath = join(_CONFIG_DIR, fname)
  971. if exists(fpath):
  972. return TuyaDeviceConfig(fname)
  973. else:
  974. return config_for_legacy_use(conf_type)
  975. def config_for_legacy_use(conf_type):
  976. """
  977. Return a config to use with config_type for legacy transition.
  978. Note: as there are two variants for Kogan Socket, this is not guaranteed
  979. to be the correct config for the device, so only use it for looking up
  980. the legacy class during the transition period.
  981. """
  982. for cfg in available_configs():
  983. parsed = TuyaDeviceConfig(cfg)
  984. if parsed.legacy_type == conf_type:
  985. return parsed
  986. return None