mixin.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. self._attr_translation_key = config.translation_key
  19. return {c.name: c for c in config.dps()}
  20. def _init_end(self, dps):
  21. for d in dps.values():
  22. if not d.hidden:
  23. self._attr_dps.append(d)
  24. @property
  25. def should_poll(self):
  26. return False
  27. @property
  28. def available(self):
  29. return self._device.has_returned_state
  30. @property
  31. def has_entity_name(self):
  32. return True
  33. @property
  34. def name(self):
  35. """Return the name for the UI."""
  36. super_name = getattr(super(), "name")
  37. return self._config.name or super_name
  38. @property
  39. def unique_id(self):
  40. """Return the unique id for this entity."""
  41. return self._config.unique_id(self._device.unique_id)
  42. @property
  43. def device_info(self):
  44. """Return the device's information."""
  45. return self._device.device_info
  46. @property
  47. def entity_category(self):
  48. """Return the entitiy's category."""
  49. return (
  50. None
  51. if self._config.entity_category is None
  52. else EntityCategory(self._config.entity_category)
  53. )
  54. @property
  55. def icon(self):
  56. """Return the icon to use in the frontend for this device."""
  57. icon = self._config.icon(self._device)
  58. if icon:
  59. return icon
  60. else:
  61. return super().icon
  62. @property
  63. def extra_state_attributes(self):
  64. """Get additional attributes that the platform itself does not support."""
  65. attr = {}
  66. for a in self._attr_dps:
  67. value = a.get_value(self._device)
  68. if value is not None or not a.optional:
  69. attr[a.name] = value
  70. return attr
  71. async def async_update(self):
  72. await self._device.async_refresh()
  73. async def async_added_to_hass(self):
  74. self._device.register_entity(self)
  75. async def async_will_remove_from_hass(self):
  76. await self._device.async_unregister_entity(self)
  77. UNIT_ASCII_MAP = {
  78. "C": UnitOfTemperature.CELSIUS,
  79. "F": UnitOfTemperature.FAHRENHEIT,
  80. "ugm3": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
  81. "m2": AREA_SQUARE_METERS,
  82. }
  83. def unit_from_ascii(unit):
  84. if unit in UNIT_ASCII_MAP:
  85. return UNIT_ASCII_MAP[unit]
  86. return unit