2019-10-23 06:31:43 +00:00
|
|
|
"""Platform for solarlog sensors."""
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-12 22:09:15 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-03-21 14:24:05 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-10-23 06:31:43 +00:00
|
|
|
|
2021-08-27 21:59:55 +00:00
|
|
|
from . import SolarlogData
|
|
|
|
from .const import DOMAIN, SENSOR_TYPES, SolarLogSensorEntityDescription
|
2019-10-23 06:31:43 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:06:08 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-10-23 06:31:43 +00:00
|
|
|
"""Add solarlog entry."""
|
2021-08-27 21:59:55 +00:00
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
SolarlogSensor(coordinator, description) for description in SENSOR_TYPES
|
|
|
|
)
|
2021-08-23 20:11:20 +00:00
|
|
|
|
|
|
|
|
2022-03-21 14:24:05 +00:00
|
|
|
class SolarlogSensor(CoordinatorEntity[SolarlogData], SensorEntity):
|
2021-08-23 20:11:20 +00:00
|
|
|
"""Representation of a Sensor."""
|
|
|
|
|
2021-08-27 21:59:55 +00:00
|
|
|
entity_description: SolarLogSensorEntityDescription
|
|
|
|
|
2021-08-23 20:11:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-08-27 21:59:55 +00:00
|
|
|
coordinator: SolarlogData,
|
2021-08-23 20:11:20 +00:00
|
|
|
description: SolarLogSensorEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
2021-08-27 21:59:55 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-23 20:11:20 +00:00
|
|
|
self.entity_description = description
|
2021-08-27 21:59:55 +00:00
|
|
|
self._attr_name = f"{coordinator.name} {description.name}"
|
|
|
|
self._attr_unique_id = f"{coordinator.unique_id}_{description.key}"
|
2021-10-27 11:57:45 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, coordinator.unique_id)},
|
|
|
|
manufacturer="Solar-Log",
|
|
|
|
name=coordinator.name,
|
2021-11-02 18:27:19 +00:00
|
|
|
configuration_url=coordinator.host,
|
2021-10-27 11:57:45 +00:00
|
|
|
)
|
2021-08-23 20:11:20 +00:00
|
|
|
|
2021-08-27 21:59:55 +00:00
|
|
|
@property
|
2021-12-12 22:09:15 +00:00
|
|
|
def native_value(self):
|
2021-08-27 21:59:55 +00:00
|
|
|
"""Return the native sensor value."""
|
2021-12-17 11:40:32 +00:00
|
|
|
raw_attr = getattr(self.coordinator.data, self.entity_description.key)
|
|
|
|
if self.entity_description.value:
|
|
|
|
return self.entity_description.value(raw_attr)
|
|
|
|
return raw_attr
|