entity.py 3.9 KB

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