mixin.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. Mixins to make writing new platforms easier
  3. """
  4. import logging
  5. from homeassistant.const import (
  6. AREA_SQUARE_METERS,
  7. CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
  8. UnitOfTemperature,
  9. )
  10. from homeassistant.helpers.entity import EntityCategory
  11. _LOGGER = logging.getLogger(__name__)
  12. class TuyaLocalEntity:
  13. """Common functions for all entity types."""
  14. def _init_begin(self, device, config):
  15. self._device = device
  16. self._config = config
  17. self._attr_dps = []
  18. return {c.name: c for c in config.dps()}
  19. def _init_end(self, dps):
  20. for d in dps.values():
  21. if not d.hidden:
  22. self._attr_dps.append(d)
  23. @property
  24. def should_poll(self):
  25. return False
  26. @property
  27. def available(self):
  28. return self._device.has_returned_state
  29. @property
  30. def name(self):
  31. """Return the name for the UI."""
  32. return self._config.name
  33. @property
  34. def translation_key(self):
  35. """Return the translation key."""
  36. return self._config.translation_key
  37. @property
  38. def has_entity_name(self):
  39. return True
  40. @property
  41. def unique_id(self):
  42. """Return the unique id for this entity."""
  43. return self._config.unique_id(self._device.unique_id)
  44. @property
  45. def device_info(self):
  46. """Return the device's information."""
  47. return self._device.device_info
  48. @property
  49. def entity_category(self):
  50. """Return the entitiy's category."""
  51. return (
  52. None
  53. if self._config.entity_category is None
  54. else EntityCategory(self._config.entity_category)
  55. )
  56. @property
  57. def icon(self):
  58. """Return the icon to use in the frontend for this device."""
  59. icon = self._config.icon(self._device)
  60. if icon:
  61. return icon
  62. else:
  63. return super().icon
  64. @property
  65. def extra_state_attributes(self):
  66. """Get additional attributes that the platform itself does not support."""
  67. attr = {}
  68. for a in self._attr_dps:
  69. value = a.get_value(self._device)
  70. if value is not None or not a.optional:
  71. attr[a.name] = value
  72. return attr
  73. async def async_update(self):
  74. await self._device.async_refresh()
  75. async def async_added_to_hass(self):
  76. self._device.register_entity(self)
  77. async def async_will_remove_from_hass(self):
  78. await self._device.async_unregister_entity(self)
  79. UNIT_ASCII_MAP = {
  80. "C": UnitOfTemperature.CELSIUS,
  81. "F": UnitOfTemperature.FAHRENHEIT,
  82. "ugm3": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
  83. "m2": AREA_SQUARE_METERS,
  84. }
  85. def unit_from_ascii(unit):
  86. if unit in UNIT_ASCII_MAP:
  87. return UNIT_ASCII_MAP[unit]
  88. return unit