sensor.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. """Support for Gardena Smart System sensors."""
  2. import logging
  3. from homeassistant.const import (
  4. DEVICE_CLASS_HUMIDITY,
  5. DEVICE_CLASS_ILLUMINANCE,
  6. DEVICE_CLASS_TEMPERATURE,
  7. TEMP_CELSIUS,
  8. )
  9. from homeassistant.helpers.entity import Entity
  10. from homeassistant.core import callback
  11. from homeassistant.const import (
  12. ATTR_BATTERY_LEVEL,
  13. DEVICE_CLASS_BATTERY,
  14. PERCENTAGE,
  15. )
  16. from homeassistant.helpers.typing import ConfigType, HomeAssistantType
  17. from .const import (
  18. DOMAIN,
  19. ATTR_BATTERY_STATE,
  20. ATTR_RF_LINK_LEVEL,
  21. ATTR_RF_LINK_STATE,
  22. ATTR_SERIAL,
  23. GARDENA_LOCATION,
  24. )
  25. _LOGGER = logging.getLogger(__name__)
  26. SOIL_SENSOR_TYPES = {
  27. "soil_temperature": [TEMP_CELSIUS, "mdi:thermometer", DEVICE_CLASS_TEMPERATURE],
  28. "soil_humidity": ["%", "mdi:water-percent", DEVICE_CLASS_HUMIDITY],
  29. ATTR_BATTERY_LEVEL: [PERCENTAGE, "mdi:battery", DEVICE_CLASS_BATTERY],
  30. }
  31. SENSOR_TYPES = {**{
  32. "ambient_temperature": [TEMP_CELSIUS, "mdi:thermometer", DEVICE_CLASS_TEMPERATURE],
  33. "light_intensity": ["lx", None, DEVICE_CLASS_ILLUMINANCE],
  34. }, **SOIL_SENSOR_TYPES}
  35. async def async_setup_entry(hass, config_entry, async_add_entities):
  36. """Perform the setup for Gardena sensor devices."""
  37. entities = []
  38. for sensor in hass.data[DOMAIN][GARDENA_LOCATION].find_device_by_type("SENSOR"):
  39. for sensor_type in SENSOR_TYPES:
  40. entities.append(GardenaSensor(sensor, sensor_type))
  41. for sensor in hass.data[DOMAIN][GARDENA_LOCATION].find_device_by_type("SOIL_SENSOR"):
  42. for sensor_type in SOIL_SENSOR_TYPES:
  43. entities.append(GardenaSensor(sensor, sensor_type))
  44. for mower in hass.data[DOMAIN][GARDENA_LOCATION].find_device_by_type("MOWER"):
  45. # Add battery sensor for mower
  46. entities.append(GardenaSensor(mower, ATTR_BATTERY_LEVEL))
  47. for water_control in hass.data[DOMAIN][GARDENA_LOCATION].find_device_by_type("WATER_CONTROL"):
  48. # Add battery sensor for water control
  49. entities.append(GardenaSensor(water_control, ATTR_BATTERY_LEVEL))
  50. _LOGGER.debug("Adding sensor as sensor %s", entities)
  51. async_add_entities(entities, True)
  52. class GardenaSensor(Entity):
  53. """Representation of a Gardena Sensor."""
  54. def __init__(self, device, sensor_type):
  55. """Initialize the Gardena Sensor."""
  56. self._sensor_type = sensor_type
  57. self._name = f"{device.name} {sensor_type.replace('_', ' ')}"
  58. self._unique_id = f"{device.serial}-{sensor_type}"
  59. self._device = device
  60. async def async_added_to_hass(self):
  61. """Subscribe to sensor events."""
  62. self._device.add_callback(self.update_callback)
  63. @property
  64. def should_poll(self) -> bool:
  65. """No polling needed for a sensor."""
  66. return False
  67. def update_callback(self, device):
  68. """Call update for Home Assistant when the device is updated."""
  69. self.schedule_update_ha_state(True)
  70. @property
  71. def name(self):
  72. """Return the name of the device."""
  73. return self._name
  74. @property
  75. def unique_id(self) -> str:
  76. """Return a unique ID."""
  77. return self._unique_id
  78. @property
  79. def icon(self):
  80. """Return the icon to use in the frontend."""
  81. return SENSOR_TYPES[self._sensor_type][1]
  82. @property
  83. def unit_of_measurement(self):
  84. """Return the unit of measurement of this entity, if any."""
  85. return SENSOR_TYPES[self._sensor_type][0]
  86. @property
  87. def device_class(self):
  88. """Return the device class of this entity."""
  89. if self._sensor_type in SENSOR_TYPES:
  90. return SENSOR_TYPES[self._sensor_type][2]
  91. return None
  92. @property
  93. def state(self):
  94. """Return the state of the sensor."""
  95. return getattr(self._device, self._sensor_type)
  96. @property
  97. def extra_state_attributes(self):
  98. """Return the state attributes of the sensor."""
  99. return {
  100. ATTR_BATTERY_LEVEL: self._device.battery_level,
  101. ATTR_BATTERY_STATE: self._device.battery_state,
  102. ATTR_RF_LINK_LEVEL: self._device.rf_link_level,
  103. ATTR_RF_LINK_STATE: self._device.rf_link_state,
  104. }
  105. @property
  106. def device_info(self):
  107. return {
  108. "identifiers": {
  109. # Serial numbers are unique identifiers within a specific domain
  110. (DOMAIN, self._device.serial)
  111. },
  112. "name": self._device.name,
  113. "manufacturer": "Gardena",
  114. "model": self._device.model_type,
  115. }