2019-11-09 20:18:41 +01:00
|
|
|
"""Support for WLED sensors."""
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2020-03-13 13:19:05 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-02-13 18:52:58 +02:00
|
|
|
from homeassistant.const import DATA_BYTES, DEVICE_CLASS_TIMESTAMP
|
2019-11-09 20:18:41 +01:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
from . import WLEDDataUpdateCoordinator, WLEDDeviceEntity
|
|
|
|
from .const import ATTR_LED_COUNT, ATTR_MAX_POWER, CURRENT_MA, DOMAIN
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up WLED sensor based on a config entry."""
|
2020-03-13 13:19:05 +01:00
|
|
|
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
sensors = [
|
2020-03-13 13:19:05 +01:00
|
|
|
WLEDEstimatedCurrentSensor(entry.entry_id, coordinator),
|
|
|
|
WLEDUptimeSensor(entry.entry_id, coordinator),
|
|
|
|
WLEDFreeHeapSensor(entry.entry_id, coordinator),
|
2019-11-09 20:18:41 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
|
|
|
class WLEDSensor(WLEDDeviceEntity):
|
|
|
|
"""Defines a WLED sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2020-03-13 13:19:05 +01:00
|
|
|
*,
|
|
|
|
coordinator: WLEDDataUpdateCoordinator,
|
|
|
|
enabled_default: bool = True,
|
2019-11-09 20:18:41 +01:00
|
|
|
entry_id: str,
|
|
|
|
icon: str,
|
|
|
|
key: str,
|
2020-03-13 13:19:05 +01:00
|
|
|
name: str,
|
|
|
|
unit_of_measurement: Optional[str] = None,
|
2019-11-09 20:18:41 +01:00
|
|
|
) -> None:
|
|
|
|
"""Initialize WLED sensor."""
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
|
|
|
self._key = key
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
super().__init__(
|
|
|
|
entry_id=entry_id,
|
|
|
|
coordinator=coordinator,
|
|
|
|
name=name,
|
|
|
|
icon=icon,
|
|
|
|
enabled_default=enabled_default,
|
|
|
|
)
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique ID for this sensor."""
|
2020-03-13 13:19:05 +01:00
|
|
|
return f"{self.coordinator.data.info.mac_address}_{self._key}"
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit this state is expressed in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
|
|
|
|
class WLEDEstimatedCurrentSensor(WLEDSensor):
|
|
|
|
"""Defines a WLED estimated current sensor."""
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
def __init__(self, entry_id: str, coordinator: WLEDDataUpdateCoordinator) -> None:
|
2019-11-09 20:18:41 +01:00
|
|
|
"""Initialize WLED estimated current sensor."""
|
|
|
|
super().__init__(
|
2020-03-13 13:19:05 +01:00
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry_id,
|
|
|
|
icon="mdi:power",
|
|
|
|
key="estimated_current",
|
|
|
|
name=f"{coordinator.data.info.name} Estimated Current",
|
|
|
|
unit_of_measurement=CURRENT_MA,
|
2019-11-09 20:18:41 +01:00
|
|
|
)
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self) -> Optional[Dict[str, Any]]:
|
|
|
|
"""Return the state attributes of the entity."""
|
|
|
|
return {
|
|
|
|
ATTR_LED_COUNT: self.coordinator.data.info.leds.count,
|
|
|
|
ATTR_MAX_POWER: self.coordinator.data.info.leds.max_power,
|
2019-11-09 20:18:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
@property
|
|
|
|
def state(self) -> Union[None, str, int, float]:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.coordinator.data.info.leds.power
|
|
|
|
|
2019-11-09 20:18:41 +01:00
|
|
|
|
|
|
|
class WLEDUptimeSensor(WLEDSensor):
|
|
|
|
"""Defines a WLED uptime sensor."""
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
def __init__(self, entry_id: str, coordinator: WLEDDataUpdateCoordinator) -> None:
|
2019-11-09 20:18:41 +01:00
|
|
|
"""Initialize WLED uptime sensor."""
|
|
|
|
super().__init__(
|
2020-03-13 13:19:05 +01:00
|
|
|
coordinator=coordinator,
|
2020-01-22 10:45:38 +01:00
|
|
|
enabled_default=False,
|
2020-03-13 13:19:05 +01:00
|
|
|
entry_id=entry_id,
|
|
|
|
icon="mdi:clock-outline",
|
|
|
|
key="uptime",
|
|
|
|
name=f"{coordinator.data.info.name} Uptime",
|
2019-11-09 20:18:41 +01:00
|
|
|
)
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
@property
|
|
|
|
def state(self) -> Union[None, str, int, float]:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
uptime = utcnow() - timedelta(seconds=self.coordinator.data.info.uptime)
|
|
|
|
return uptime.replace(microsecond=0).isoformat()
|
|
|
|
|
2019-11-09 20:18:41 +01:00
|
|
|
@property
|
|
|
|
def device_class(self) -> Optional[str]:
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
return DEVICE_CLASS_TIMESTAMP
|
|
|
|
|
|
|
|
|
|
|
|
class WLEDFreeHeapSensor(WLEDSensor):
|
|
|
|
"""Defines a WLED free heap sensor."""
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
def __init__(self, entry_id: str, coordinator: WLEDDataUpdateCoordinator) -> None:
|
2019-11-09 20:18:41 +01:00
|
|
|
"""Initialize WLED free heap sensor."""
|
|
|
|
super().__init__(
|
2020-03-13 13:19:05 +01:00
|
|
|
coordinator=coordinator,
|
2020-01-22 10:45:38 +01:00
|
|
|
enabled_default=False,
|
2020-03-13 13:19:05 +01:00
|
|
|
entry_id=entry_id,
|
|
|
|
icon="mdi:memory",
|
|
|
|
key="free_heap",
|
|
|
|
name=f"{coordinator.data.info.name} Free Memory",
|
|
|
|
unit_of_measurement=DATA_BYTES,
|
2019-11-09 20:18:41 +01:00
|
|
|
)
|
|
|
|
|
2020-03-13 13:19:05 +01:00
|
|
|
@property
|
|
|
|
def state(self) -> Union[None, str, int, float]:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.coordinator.data.info.free_heap
|