2020-12-17 15:46:22 +00:00
|
|
|
"""Support for Somfy Thermostat Battery."""
|
|
|
|
|
|
|
|
from pymfy.api.devices.category import Category
|
|
|
|
from pymfy.api.devices.thermostat import Thermostat
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-12-17 15:46:22 +00:00
|
|
|
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
|
|
|
|
|
|
|
|
from . import SomfyEntity
|
|
|
|
from .const import API, COORDINATOR, DOMAIN
|
|
|
|
|
|
|
|
SUPPORTED_CATEGORIES = {Category.HVAC.value}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2020-12-24 19:42:56 +00:00
|
|
|
"""Set up the Somfy sensor platform."""
|
|
|
|
domain_data = hass.data[DOMAIN]
|
|
|
|
coordinator = domain_data[COORDINATOR]
|
|
|
|
api = domain_data[API]
|
2020-12-17 15:46:22 +00:00
|
|
|
|
2020-12-24 19:42:56 +00:00
|
|
|
sensors = [
|
|
|
|
SomfyThermostatBatterySensor(coordinator, device_id, api)
|
|
|
|
for device_id, device in coordinator.data.items()
|
|
|
|
if SUPPORTED_CATEGORIES & set(device.categories)
|
|
|
|
]
|
2020-12-17 15:46:22 +00:00
|
|
|
|
2020-12-24 19:42:56 +00:00
|
|
|
async_add_entities(sensors)
|
2020-12-17 15:46:22 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SomfyThermostatBatterySensor(SomfyEntity, SensorEntity):
|
2020-12-17 15:46:22 +00:00
|
|
|
"""Representation of a Somfy thermostat battery."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator, device_id, api):
|
|
|
|
"""Initialize the Somfy device."""
|
|
|
|
super().__init__(coordinator, device_id, api)
|
|
|
|
self._climate = None
|
|
|
|
self._create_device()
|
|
|
|
|
|
|
|
def _create_device(self):
|
|
|
|
"""Update the device with the latest data."""
|
|
|
|
self._climate = Thermostat(self.device, self.api)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> int:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._climate.get_battery()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self) -> str:
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit of measurement of the sensor."""
|
|
|
|
return PERCENTAGE
|