mixin.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. Mixins to make writing new platforms easier
  3. """
  4. import logging
  5. _LOGGER = logging.getLogger(__name__)
  6. class TuyaLocalEntity:
  7. """Common functions for all entity types."""
  8. def _init_begin(self, device, config):
  9. self._device = device
  10. self._config = config
  11. self._attr_dps = []
  12. return {c.name: c for c in config.dps()}
  13. def _init_end(self, dps):
  14. for d in dps.values():
  15. if not d.hidden:
  16. self._attr_dps.append(d)
  17. @property
  18. def should_poll(self):
  19. return True
  20. @property
  21. def available(self):
  22. return self._device.has_returned_state
  23. @property
  24. def name(self):
  25. """Return the name for the UI."""
  26. return self._config.name(self._device.name)
  27. @property
  28. def unique_id(self):
  29. """Return the unique id for this entity."""
  30. return self._config.unique_id(self._device.unique_id)
  31. @property
  32. def device_info(self):
  33. """Return the device's information."""
  34. return self._device.device_info
  35. @property
  36. def entity_category(self):
  37. """Return the entitiy's category."""
  38. return self._config.entity_category
  39. @property
  40. def icon(self):
  41. """Return the icon to use in the frontend for this device."""
  42. icon = self._config.icon(self._device)
  43. if icon:
  44. return icon
  45. else:
  46. return super().icon
  47. @property
  48. def device_state_attributes(self):
  49. """Get additional attributes that the platform itself does not support."""
  50. attr = {}
  51. for a in self._attr_dps:
  52. attr[a.name] = a.get_value(self._device)
  53. return attr
  54. async def async_update(self):
  55. await self._device.async_refresh()