|
| 1 | +"""Binary Sensor for power energy.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from homeassistant.components.binary_sensor import ( |
| 7 | + BinarySensorDeviceClass, |
| 8 | + BinarySensorEntity, |
| 9 | +) |
| 10 | +from homeassistant.config_entries import ConfigEntry |
| 11 | +from homeassistant.core import HomeAssistant |
| 12 | +from homeassistant.helpers.entity import DeviceInfo, EntityCategory |
| 13 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 14 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 15 | + |
| 16 | +from .const import DOMAIN |
| 17 | + |
| 18 | +_LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +async def async_setup_entry( |
| 22 | + hass: HomeAssistant, |
| 23 | + config_entry: ConfigEntry, |
| 24 | + async_add_entities: AddEntitiesCallback, |
| 25 | +) -> None: |
| 26 | + """Set up the sensors.""" |
| 27 | + coordinator = hass.data[DOMAIN][config_entry.entry_id] |
| 28 | + async_add_entities([CountdownSensor(coordinator)]) |
| 29 | + |
| 30 | + |
| 31 | +class CountdownSensor(CoordinatorEntity, BinarySensorEntity): |
| 32 | + """Sensor return token expiration date.""" |
| 33 | + |
| 34 | + _attr_device_class = BinarySensorDeviceClass.PROBLEM |
| 35 | + _attr_name = "MyElectricalData Token" |
| 36 | + _attr_has_entity_name = True |
| 37 | + _attr_entity_category = EntityCategory.DIAGNOSTIC |
| 38 | + |
| 39 | + def __init__(self, coordinator): |
| 40 | + """Initialize the sensor.""" |
| 41 | + super().__init__(coordinator) |
| 42 | + access = coordinator.access |
| 43 | + self._attr_unique_id = f"{coordinator.pdl}_token_expire" |
| 44 | + self._attr_extra_state_attributes = { |
| 45 | + "Call number": access.get("call_number"), |
| 46 | + "Last call": access.get("last_call"), |
| 47 | + "Banned": access.get("ban"), |
| 48 | + "Quota": access.get("quota_limit"), |
| 49 | + "Quota reached": access.get("quota_reached"), |
| 50 | + "Expiration date": access.get("consent_expiration_date"), |
| 51 | + } |
| 52 | + self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, coordinator.pdl)}) |
| 53 | + |
| 54 | + @property |
| 55 | + def is_on(self): |
| 56 | + """Value power.""" |
| 57 | + return self.coordinator.access.get("valid", False) is False |
0 commit comments