device_config.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. # Get raw value directly avoiding accidental scaling by decoded_value()
  397. raw_from_device = device.get_property(self.id)
  398. bytevalue = self.decode_value(raw_from_device, device)
  399. if mask and isinstance(bytevalue, bytes):
  400. value = int.from_bytes(bytevalue, self.endianness)
  401. scale = mask & (1 + ~mask)
  402. raw_result = (value & mask) // scale
  403. # Insert signed interpretation here
  404. if self._config.get("mask_signed", False):
  405. # Count how many bits are set in the mask
  406. bit_count = mask.bit_count()
  407. raw_result = to_signed(raw_result, bit_count)
  408. return self._map_from_dps(raw_result, device)
  409. elif mask and isinstance(bytevalue, int):
  410. # Handle masking for integer DPs
  411. scale = mask & (1 + ~mask)
  412. raw_result = (bytevalue & mask) // scale
  413. return self._map_from_dps(raw_result, device)
  414. else:
  415. return self._map_from_dps(raw_from_device, device)
  416. def decoded_value(self, device):
  417. v = self._map_from_dps(device.get_property(self.id), device)
  418. return self.decode_value(v, device)
  419. def decode_value(self, v, device):
  420. if self.rawtype == "hex" and isinstance(v, str):
  421. try:
  422. return bytes.fromhex(v)
  423. except ValueError:
  424. _LOGGER.warning(
  425. "%s sent invalid hex '%s' for %s",
  426. device.name,
  427. v,
  428. self.name,
  429. )
  430. return None
  431. elif self.rawtype == "base64" and isinstance(v, str):
  432. try:
  433. return b64decode(v)
  434. except ValueError:
  435. _LOGGER.warning(
  436. "%s sent invalid base64 '%s' for %s",
  437. device.name,
  438. v,
  439. self.name,
  440. )
  441. return None
  442. else:
  443. return v
  444. def encode_value(self, v):
  445. if self.rawtype == "hex":
  446. return v.hex()
  447. elif self.rawtype == "base64":
  448. return b64encode(v).decode("utf-8")
  449. elif self.rawtype == "unixtime" and isinstance(v, datetime):
  450. return v.timestamp()
  451. else:
  452. return v
  453. def _match(self, matchdata, value):
  454. """Return true val1 matches val2"""
  455. if self.rawtype == "bitfield" and matchdata:
  456. try:
  457. return (int(value) & int(matchdata)) != 0
  458. except (TypeError, ValueError):
  459. return False
  460. else:
  461. return str(value) == str(matchdata)
  462. async def async_set_value(self, device, value):
  463. """Set the value of the dps in the given device to given value."""
  464. if self.readonly:
  465. raise TypeError(f"{self.name} is read only")
  466. if self.invalid_for(value, device):
  467. raise AttributeError(f"{self.name} cannot be set at this time")
  468. settings = self.get_values_to_set(device, value)
  469. await device.async_set_properties(settings)
  470. def mapping_available(self, mapping, device):
  471. """Determine if this mapping should be available."""
  472. if "available" in mapping:
  473. avail_dp = self._entity.find_dps(mapping.get("available"))
  474. if avail_dp:
  475. return avail_dp.get_value(device)
  476. return True
  477. def should_show_mapping(self, mapping, device):
  478. """Determine if this mapping should be shown in the list of values."""
  479. if "value" not in mapping or mapping.get("hidden", False):
  480. return False
  481. return self.mapping_available(mapping, device)
  482. def values(self, device):
  483. """Return the possible values a dps can take."""
  484. if "mapping" not in self._config.keys():
  485. return []
  486. val = []
  487. for m in self._config["mapping"]:
  488. if self.should_show_mapping(m, device):
  489. val.append(m["value"])
  490. # If there is mirroring without override, include mirrored values
  491. elif "value_mirror" in m:
  492. r_dps = self._entity.find_dps(m["value_mirror"])
  493. if r_dps:
  494. val = val + r_dps.values(device)
  495. for c in m.get("conditions", {}):
  496. if self.should_show_mapping(c, device):
  497. val.append(c["value"])
  498. elif "value_mirror" in c:
  499. r_dps = self._entity.find_dps(c["value_mirror"])
  500. if r_dps:
  501. val = val + r_dps.values(device)
  502. cond = self._active_condition(m, device)
  503. if cond and "mapping" in cond:
  504. c_val = []
  505. for m2 in cond["mapping"]:
  506. if self.should_show_mapping(m2, device):
  507. c_val.append(m2["value"])
  508. elif "value_mirror" in m:
  509. r_dps = self._entity.find_dps(m["value_mirror"])
  510. if r_dps:
  511. c_val = c_val + r_dps.values(device)
  512. # if given, the conditional mapping is an override
  513. if c_val:
  514. val = c_val
  515. break
  516. return _remove_duplicates(val)
  517. @property
  518. def default(self):
  519. """Return the default value for a dp."""
  520. if "mapping" not in self._config.keys():
  521. _LOGGER.debug(
  522. "No mapping for %s, unable to determine default value",
  523. self.name,
  524. )
  525. return None
  526. for m in self._config["mapping"]:
  527. if m.get("default", False):
  528. return m.get("value", m.get("dps_val", None))
  529. for c in m.get("conditions", {}):
  530. if c.get("default", False):
  531. return c.get("value", m.get("value", m.get("dps_val", None)))
  532. def range(self, device, scaled=True):
  533. """Return the range for this dps if configured."""
  534. scale = self.scale(device) if scaled else 1
  535. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  536. r = self._config.get("range")
  537. if mapping:
  538. r = mapping.get("range", r)
  539. cond = self._active_condition(mapping, device)
  540. if cond:
  541. r = cond.get("range", r)
  542. if r and "min" in r and "max" in r:
  543. return _scale_range(r, scale)
  544. else:
  545. return None
  546. def scale(self, device):
  547. scale = 1
  548. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  549. if mapping:
  550. scale = mapping.get("scale", 1)
  551. cond = self._active_condition(mapping, device)
  552. if cond:
  553. scale = cond.get("scale", scale)
  554. return scale
  555. def precision(self, device):
  556. if self.type is int:
  557. scale = self.scale(device)
  558. precision = 0
  559. while scale > 1.0:
  560. scale /= 10.0
  561. precision += 1
  562. return precision
  563. @property
  564. def suggested_display_precision(self):
  565. return self._config.get("precision")
  566. def step(self, device, scaled=True):
  567. step = 1
  568. scale = self.scale(device) if scaled else 1
  569. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  570. if mapping:
  571. step = mapping.get("step", 1)
  572. cond = self._active_condition(mapping, device)
  573. if cond:
  574. step = cond.get("step", step)
  575. if step != 1 or scale != 1:
  576. _LOGGER.debug(
  577. "Step for %s is %s with scale %s",
  578. self.name,
  579. step,
  580. scale,
  581. )
  582. return step / scale if scaled else step
  583. @property
  584. def readonly(self):
  585. return self._config.get("readonly", False)
  586. def invalid_for(self, value, device):
  587. mapping = self._find_map_for_value(value, device)
  588. if mapping:
  589. cond = self._active_condition(mapping, device)
  590. if cond:
  591. return cond.get("invalid", False)
  592. return False
  593. @property
  594. def hidden(self):
  595. return self._config.get("hidden", False)
  596. @property
  597. def unit(self):
  598. return self._config.get("unit")
  599. @property
  600. def state_class(self):
  601. """The state class of this measurement."""
  602. return self._config.get("class")
  603. def _find_map_for_dps(self, value, device):
  604. default = None
  605. for m in self._config.get("mapping", {}):
  606. if not self.mapping_available(m, device) and "conditions" not in m:
  607. continue
  608. if "dps_val" not in m:
  609. default = m
  610. elif self._match(m["dps_val"], value):
  611. return m
  612. return default
  613. def _correct_type(self, result):
  614. """Convert value to the correct type for this dp."""
  615. if self.type is int:
  616. _LOGGER.debug("Rounding %s", self.name)
  617. result = int(round(result))
  618. elif self.type is bool:
  619. result = True if result else False
  620. elif self.type is float:
  621. result = float(result)
  622. elif self.type is str:
  623. result = str(result)
  624. if self.rawtype == "utf16b64":
  625. result = b64encode(result.encode("utf-16-be")).decode("utf-8")
  626. if self.stringify:
  627. result = str(result)
  628. return result
  629. def _map_from_dps(self, val, device):
  630. if val is not None and self.type is not str and isinstance(val, str):
  631. try:
  632. val = self.type(val)
  633. self.stringify = True
  634. except ValueError:
  635. self.stringify = False
  636. else:
  637. self.stringify = False
  638. # decode utf-16 base64 strings first, so normal strings can be matched
  639. if self.rawtype == "utf16b64" and isinstance(val, str):
  640. try:
  641. val = b64decode(val).decode("utf-16-be")
  642. except ValueError:
  643. _LOGGER.warning("Invalid utf16b64 %s", val)
  644. result = val
  645. scale = self.scale(device)
  646. replaced = False
  647. mapping = self._find_map_for_dps(val, device)
  648. if mapping:
  649. invert = mapping.get("invert", False)
  650. redirect = mapping.get("value_redirect")
  651. mirror = mapping.get("value_mirror")
  652. replaced = "value" in mapping
  653. result = mapping.get("value", result)
  654. target_range = mapping.get("target_range")
  655. cond = self._active_condition(mapping, device)
  656. if cond:
  657. if cond.get("invalid", False):
  658. return None
  659. replaced = replaced or "value" in cond
  660. result = cond.get("value", result)
  661. redirect = cond.get("value_redirect", redirect)
  662. mirror = cond.get("value_mirror", mirror)
  663. target_range = cond.get("target_range", target_range)
  664. for m in cond.get("mapping", {}):
  665. if str(m.get("dps_val")) == str(result):
  666. replaced = "value" in m
  667. result = m.get("value", result)
  668. if redirect:
  669. _LOGGER.debug("Redirecting %s to %s", self.name, redirect)
  670. r_dps = self._entity.find_dps(redirect)
  671. if r_dps:
  672. return r_dps.get_value(device)
  673. if mirror:
  674. r_dps = self._entity.find_dps(mirror)
  675. if r_dps:
  676. return r_dps.get_value(device)
  677. if invert and isinstance(result, Number):
  678. r = self._config.get("range")
  679. if r and "min" in r and "max" in r:
  680. result = -1 * result + r["min"] + r["max"]
  681. replaced = True
  682. if target_range and isinstance(result, Number):
  683. r = self._config.get("range")
  684. if r and "max" in r and "max" in target_range:
  685. from_min = r.get("min", 0)
  686. from_max = r["max"]
  687. to_min = target_range.get("min", 0)
  688. to_max = target_range["max"]
  689. result = to_min + (
  690. (result - from_min) * (to_max - to_min) / (from_max - from_min)
  691. )
  692. replaced = True
  693. if scale != 1 and isinstance(result, Number):
  694. result = result / scale
  695. replaced = True
  696. if self.rawtype == "unixtime" and isinstance(result, int):
  697. try:
  698. result = datetime.fromtimestamp(result)
  699. replaced = True
  700. except Exception:
  701. _LOGGER.warning("Invalid timestamp %d", result)
  702. if replaced:
  703. _LOGGER.debug(
  704. "%s: Mapped dps %s value from %s to %s",
  705. self._entity._device.name,
  706. self.id,
  707. val,
  708. result,
  709. )
  710. return result
  711. def _find_map_for_value(self, value, device):
  712. default = None
  713. nearest = None
  714. distance = float("inf")
  715. for m in self._config.get("mapping", {}):
  716. # no reverse mapping of hidden values
  717. ignore = m.get("hidden", False) or not self.mapping_available(m, device)
  718. if "dps_val" not in m and not ignore:
  719. default = m
  720. # The following avoids further matching on the above case
  721. # and in the null mapping case, which is intended to be
  722. # a one-way map to prevent the entity showing as unavailable
  723. # when no value is being reported by the device.
  724. if m.get("dps_val") is None:
  725. ignore = True
  726. if "value" in m and str(m["value"]) == str(value) and not ignore:
  727. return m
  728. if (
  729. "value" in m
  730. and isinstance(m["value"], Number)
  731. and isinstance(value, Number)
  732. and not ignore
  733. ):
  734. d = abs(m["value"] - value)
  735. if d < distance:
  736. distance = d
  737. nearest = m
  738. if "value" not in m and "value_mirror" in m and not ignore:
  739. r_dps = self._entity.find_dps(m["value_mirror"])
  740. if r_dps and str(r_dps.get_value(device)) == str(value):
  741. return m
  742. for c in m.get("conditions", {}):
  743. if c.get("hidden", False) or not self.mapping_available(c, device):
  744. continue
  745. if "value" in c and str(c["value"]) == str(value):
  746. c_dp = self._entity.find_dps(m.get("constraint", self.name))
  747. # only consider the condition a match if we can change
  748. # the dp to match, or it already matches
  749. if (c_dp and c_dp.id != self.id and not c_dp.readonly) or (
  750. _equal_or_in(
  751. device.get_property(c_dp.id),
  752. c.get("dps_val"),
  753. )
  754. ):
  755. return m
  756. if "value" not in c and "value_mirror" in c:
  757. r_dps = self._entity.find_dps(c["value_mirror"])
  758. if r_dps and str(r_dps.get_value(device)) == str(value):
  759. return m
  760. if nearest:
  761. return nearest
  762. return default
  763. def _active_condition(self, mapping, device, value=None):
  764. constraint = mapping.get("constraint", self.name)
  765. conditions = mapping.get("conditions")
  766. c_match = None
  767. if constraint and conditions:
  768. c_dps = self._entity.find_dps(constraint)
  769. # base64 and hex have to be decoded
  770. c_val = (
  771. None
  772. if c_dps is None
  773. else (
  774. c_dps.get_value(device)
  775. if c_dps.rawtype == "base64" or c_dps.rawtype == "hex"
  776. else device.get_property(c_dps.id)
  777. )
  778. )
  779. for cond in conditions:
  780. if not self.mapping_available(cond, device):
  781. continue
  782. if c_val is not None and (_equal_or_in(c_val, cond.get("dps_val"))):
  783. c_match = cond
  784. # Case where matching None, need extra checks to ensure we
  785. # are not just defaulting and it is really a match
  786. elif (
  787. c_val is None
  788. and c_dps is not None
  789. and "dps_val" in cond
  790. and cond.get("dps_val") is None
  791. ):
  792. c_match = cond
  793. # when changing, another condition may become active
  794. # return that if it exists over a current condition
  795. if value is not None and value == cond.get("value"):
  796. return cond
  797. return c_match
  798. def get_values_to_set(self, device, value, pending_map={}):
  799. """Return the dps values that would be set when setting to value"""
  800. result = value
  801. dps_map = {}
  802. if self.readonly:
  803. return dps_map
  804. # Special case: if the current value has a redirect mapping,
  805. # follow that.
  806. current_value = device.get_property(self.id)
  807. current_mapping = self._find_map_for_dps(current_value, device)
  808. if current_mapping:
  809. redirect = current_mapping.get("value_redirect")
  810. if redirect:
  811. return self._entity.find_dps(redirect).get_values_to_set(
  812. device,
  813. value,
  814. )
  815. # If no redirect, we need to check for mapped values in reverse
  816. mapping = self._find_map_for_value(value, device)
  817. scale = self.scale(device)
  818. mask = self.mask
  819. if mapping:
  820. replaced = False
  821. redirect = mapping.get("value_redirect")
  822. invert = mapping.get("invert", False)
  823. target_range = mapping.get("target_range")
  824. step = mapping.get("step")
  825. if not isinstance(step, Number):
  826. step = None
  827. if "dps_val" in mapping:
  828. result = mapping["dps_val"]
  829. replaced = True
  830. # Conditions may have side effect of setting another value.
  831. cond = self._active_condition(mapping, device, value)
  832. if cond:
  833. cval = cond.get("value")
  834. if cval is None:
  835. r_dps = cond.get("value_mirror")
  836. if r_dps:
  837. mirror = self._entity.find_dps(r_dps)
  838. if mirror:
  839. cval = mirror.get_value(device)
  840. if cval == value:
  841. c_dps = self._entity.find_dps(mapping.get("constraint", self.name))
  842. cond_dpsval = cond.get("dps_val")
  843. single_match = isinstance(cond_dpsval, str) or (
  844. not isinstance(cond_dpsval, Sequence)
  845. )
  846. if c_dps and c_dps.id != self.id and single_match:
  847. c_val = c_dps._map_from_dps(
  848. cond.get("dps_val", device.get_property(c_dps.id)),
  849. device,
  850. )
  851. dps_map.update(
  852. c_dps.get_values_to_set(device, c_val, pending_map)
  853. )
  854. # Allow simple conditional mapping overrides
  855. for m in cond.get("mapping", {}):
  856. if m.get("value") == value and not m.get("hidden", False):
  857. result = m.get("dps_val", result)
  858. step = cond.get("step", step)
  859. redirect = cond.get("value_redirect", redirect)
  860. target_range = cond.get("target_range", target_range)
  861. if redirect:
  862. _LOGGER.debug("Redirecting %s to %s", self.name, redirect)
  863. r_dps = self._entity.find_dps(redirect)
  864. if r_dps:
  865. return r_dps.get_values_to_set(device, value)
  866. if scale != 1 and isinstance(result, Number):
  867. _LOGGER.debug("Scaling %s by %s", result, scale)
  868. result = result * scale
  869. remap = self._find_map_for_value(result, device)
  870. if (
  871. remap
  872. and "dps_val" in remap
  873. and "dps_val" not in mapping
  874. and not remap.get("hidden", False)
  875. ):
  876. result = remap["dps_val"]
  877. replaced = True
  878. if target_range and isinstance(result, Number):
  879. r = self._config.get("range")
  880. if r and "max" in r and "max" in target_range:
  881. from_min = target_range.get("min", 0)
  882. from_max = target_range["max"]
  883. to_min = r.get("min", 0)
  884. to_max = r["max"]
  885. result = to_min + (
  886. (result - from_min) * (to_max - to_min) / (from_max - from_min)
  887. )
  888. replaced = True
  889. if invert:
  890. r = self._config.get("range")
  891. if r and "min" in r and "max" in r:
  892. result = -1 * result + r["min"] + r["max"]
  893. replaced = True
  894. if step and isinstance(result, Number):
  895. _LOGGER.debug("Stepping %s to %s", result, step)
  896. result = step * round(float(result) / step)
  897. remap = self._find_map_for_value(result, device)
  898. if (
  899. remap
  900. and "dps_val" in remap
  901. and "dps_val" not in mapping
  902. and not remap.get("hidden", False)
  903. ):
  904. result = remap["dps_val"]
  905. replaced = True
  906. if replaced:
  907. _LOGGER.debug(
  908. "%s: Mapped dps %s to %s from %s",
  909. self._entity._device.name,
  910. self.id,
  911. result,
  912. value,
  913. )
  914. r = self.range(device, scaled=False)
  915. if r and isinstance(result, Number):
  916. mn = r[0]
  917. mx = r[1]
  918. if round(result) < mn or round(result) > mx:
  919. # Output scaled values in the error message
  920. r = self.range(device, scaled=True)
  921. mn = r[0]
  922. mx = r[1]
  923. raise ValueError(f"{self.name} ({value}) must be between {mn} and {mx}")
  924. if mask and isinstance(result, bool):
  925. result = int(result)
  926. if mask and isinstance(result, Number):
  927. # mask is in hex, 2 digits/characters per byte
  928. hex_mask = self._config.get("mask")
  929. length = int(len(hex_mask) / 2)
  930. # Convert to int
  931. endianness = self.endianness
  932. mask_scale = mask & (1 + ~mask)
  933. # Get raw current value directly (avoids scaling being auto applied as it causes issues)
  934. raw_current = device.get_property(self.id)
  935. if self.id in pending_map:
  936. decoded_value = self.decode_value(pending_map[self.id], device)
  937. else:
  938. decoded_value = self.decode_value(raw_current, device)
  939. if isinstance(decoded_value, int):
  940. current_value = decoded_value
  941. result = (current_value & ~mask) | (mask & int(result * mask_scale))
  942. # Only convert back to bytes if the DP is actually hex/base64
  943. if self.rawtype in ["hex", "base64", "utf16b64"]:
  944. result = self.encode_value(result.to_bytes(length, endianness))
  945. else:
  946. # Bytes path (original logic)
  947. current_value = int.from_bytes(decoded_value, endianness)
  948. result = (current_value & ~mask) | (mask & int(result * mask_scale))
  949. result = self.encode_value(result.to_bytes(length, endianness))
  950. dps_map[self.id] = self._correct_type(result)
  951. return dps_map
  952. def icon_rule(self, device):
  953. mapping = self._find_map_for_dps(device.get_property(self.id), device)
  954. icon = None
  955. priority = 100
  956. if mapping:
  957. icon = mapping.get("icon", icon)
  958. priority = mapping.get("icon_priority", 10 if icon else 100)
  959. cond = self._active_condition(mapping, device)
  960. if cond and cond.get("icon_priority", 10) < priority:
  961. icon = cond.get("icon", icon)
  962. priority = cond.get("icon_priority", 10 if icon else 100)
  963. return {"priority": priority, "icon": icon}
  964. def available_configs():
  965. """List the available config files."""
  966. _CONFIG_DIR = dirname(config_dir.__file__)
  967. for direntry in scandir(_CONFIG_DIR):
  968. if direntry.is_file() and fnmatch(direntry.name, "*.yaml"):
  969. yield direntry.name
  970. def possible_matches(dps, product_ids=None):
  971. """Return possible matching configs for a given set of
  972. dps values and product_ids."""
  973. for cfg in available_configs():
  974. parsed = TuyaDeviceConfig(cfg)
  975. try:
  976. if parsed.matches(dps, product_ids):
  977. yield parsed
  978. except TypeError:
  979. _LOGGER.error("Parse error in %s", cfg)
  980. def get_config(conf_type):
  981. """
  982. Return a config to use with config_type.
  983. """
  984. _CONFIG_DIR = dirname(config_dir.__file__)
  985. fname = conf_type + ".yaml"
  986. fpath = join(_CONFIG_DIR, fname)
  987. if exists(fpath):
  988. return TuyaDeviceConfig(fname)
  989. else:
  990. return config_for_legacy_use(conf_type)
  991. def config_for_legacy_use(conf_type):
  992. """
  993. Return a config to use with config_type for legacy transition.
  994. Note: as there are two variants for Kogan Socket, this is not guaranteed
  995. to be the correct config for the device, so only use it for looking up
  996. the legacy class during the transition period.
  997. """
  998. for cfg in available_configs():
  999. parsed = TuyaDeviceConfig(cfg)
  1000. if parsed.legacy_type == conf_type:
  1001. return parsed
  1002. return None