mixin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = (
  19. config.translation_key or config.translation_only_key
  20. )
  21. return {c.name: c for c in config.dps()}
  22. def _init_end(self, dps):
  23. for d in dps.values():
  24. if not d.hidden:
  25. self._attr_dps.append(d)
  26. @property
  27. def should_poll(self):
  28. return False
  29. @property
  30. def available(self):
  31. return self._device.has_returned_state
  32. @property
  33. def has_entity_name(self):
  34. return True
  35. @property
  36. def name(self):
  37. """Return the name for the UI."""
  38. own_name = self._config.name
  39. if not own_name and not self.use_device_name:
  40. # super has the translation logic
  41. own_name = getattr(super(), "name")
  42. return own_name
  43. @property
  44. def use_device_name(self):
  45. """Return whether to use the device name for the entity name"""
  46. own_name = self._config.name or self._config.translation_key
  47. return not own_name
  48. @property
  49. def unique_id(self):
  50. """Return the unique id for this entity."""
  51. return self._config.unique_id(self._device.unique_id)
  52. @property
  53. def device_info(self):
  54. """Return the device's information."""
  55. return self._device.device_info
  56. @property
  57. def entity_category(self):
  58. """Return the entitiy's category."""
  59. return (
  60. None
  61. if self._config.entity_category is None
  62. else EntityCategory(self._config.entity_category)
  63. )
  64. @property
  65. def icon(self):
  66. """Return the icon to use in the frontend for this device."""
  67. icon = self._config.icon(self._device)
  68. if icon:
  69. return icon
  70. else:
  71. return super().icon
  72. @property
  73. def extra_state_attributes(self):
  74. """Get additional attributes that the platform itself does not support."""
  75. attr = {}
  76. for a in self._attr_dps:
  77. value = a.get_value(self._device)
  78. if value is not None or not a.optional:
  79. attr[a.name] = value
  80. return attr
  81. @property
  82. def entity_registry_enabled_default(self):
  83. """Disable deprecated entities on new installations"""
  84. return not self._config.deprecated
  85. async def async_update(self):
  86. await self._device.async_refresh()
  87. async def async_added_to_hass(self):
  88. self._device.register_entity(self)
  89. async def async_will_remove_from_hass(self):
  90. await self._device.async_unregister_entity(self)
  91. def on_receive(self, dps, full_poll):
  92. """Override to process dps directly as they are received"""
  93. pass
  94. UNIT_ASCII_MAP = {
  95. "C": UnitOfTemperature.CELSIUS,
  96. "F": UnitOfTemperature.FAHRENHEIT,
  97. "ugm3": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
  98. "m2": AREA_SQUARE_METERS,
  99. }
  100. def unit_from_ascii(unit):
  101. if unit in UNIT_ASCII_MAP:
  102. return UNIT_ASCII_MAP[unit]
  103. return unit