mixin.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. alt_name = self._config.translation_key
  47. if self._config.translation_key is self._config.device_class:
  48. alt_name = None
  49. own_name = self._config.name or alt_name
  50. return not own_name
  51. @property
  52. def unique_id(self):
  53. """Return the unique id for this entity."""
  54. return self._config.unique_id(self._device.unique_id)
  55. @property
  56. def device_info(self):
  57. """Return the device's information."""
  58. return self._device.device_info
  59. @property
  60. def entity_category(self):
  61. """Return the entitiy's category."""
  62. return (
  63. None
  64. if self._config.entity_category is None
  65. else EntityCategory(self._config.entity_category)
  66. )
  67. @property
  68. def icon(self):
  69. """Return the icon to use in the frontend for this device."""
  70. icon = self._config.icon(self._device)
  71. if icon:
  72. return icon
  73. else:
  74. return super().icon
  75. @property
  76. def extra_state_attributes(self):
  77. """Get additional attributes that the platform itself does not support."""
  78. attr = {}
  79. for a in self._attr_dps:
  80. value = a.get_value(self._device)
  81. if value is not None or not a.optional:
  82. attr[a.name] = value
  83. return attr
  84. @property
  85. def entity_registry_enabled_default(self):
  86. """Disable deprecated entities on new installations"""
  87. return not self._config.deprecated
  88. async def async_update(self):
  89. await self._device.async_refresh()
  90. async def async_added_to_hass(self):
  91. self._device.register_entity(self)
  92. if self._config.deprecated:
  93. _LOGGER.warning(self._config.deprecation_message)
  94. async def async_will_remove_from_hass(self):
  95. await self._device.async_unregister_entity(self)
  96. def on_receive(self, dps, full_poll):
  97. """Override to process dps directly as they are received"""
  98. pass
  99. UNIT_ASCII_MAP = {
  100. "C": UnitOfTemperature.CELSIUS.value,
  101. "F": UnitOfTemperature.FAHRENHEIT.value,
  102. "ugm3": CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
  103. "m2": AREA_SQUARE_METERS,
  104. }
  105. def unit_from_ascii(unit):
  106. if unit in UNIT_ASCII_MAP:
  107. return UNIT_ASCII_MAP[unit]
  108. return unit