core/homeassistant/components/ecoforest/sensor.py

73 lines
2.0 KiB
Python
Raw Normal View History

Add ecoforest integration (#100647) * Add ecoforest integration * fix file title * remove host default from schema, hints will be given in the documentation * moved input validation to async_step_user * ensure we can receive device data while doing entry setup * remove unecessary check before unique id is set * added shorter syntax for async create entry Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use variable to set unique id Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Use _attr_has_entity_name from base entity Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * remove unecessary comments in coordinator * use shorthand for device information * remove empty objects from manifest * remove unecessary flag Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use _async_abort_entries_match to ensure device is not duplicated * remove unecessary host attr * fixed coordinator host attr to be used by entities to identify device * remove unecessary assert * use default device class temperature trasnlation key * reuse base entity description * use device serial number as identifier * remove unused code * Improve logging message Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unused errors Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Raise a generic update failed Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use coordinator directly Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * No need to check for serial number Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * rename variable Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use renamed variable Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * improve assertion Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use serial number in entity unique id Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * raise config entry not ready on setup when error in connection * improve test readability * Improve python syntax Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * abort when device already configured with same serial number * improve tests * fix test name * use coordinator data Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * improve asserts Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * fix ci * improve error handling --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-21 13:18:55 +00:00
"""Support for Ecoforest sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import logging
from pyecoforest.models.device import Device
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import EcoforestCoordinator
from .entity import EcoforestEntity
_LOGGER = logging.getLogger(__name__)
@dataclass
class EcoforestRequiredKeysMixin:
"""Mixin for required keys."""
value_fn: Callable[[Device], float | None]
@dataclass
class EcoforestSensorEntityDescription(
SensorEntityDescription, EcoforestRequiredKeysMixin
):
"""Describes Ecoforest sensor entity."""
SENSOR_TYPES: tuple[EcoforestSensorEntityDescription, ...] = (
EcoforestSensorEntityDescription(
key="temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
value_fn=lambda data: data.environment_temperature,
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the Ecoforest sensor platform."""
coordinator: EcoforestCoordinator = hass.data[DOMAIN][entry.entry_id]
entities = [
EcoforestSensor(coordinator, description) for description in SENSOR_TYPES
]
async_add_entities(entities)
class EcoforestSensor(SensorEntity, EcoforestEntity):
"""Representation of an Ecoforest sensor."""
entity_description: EcoforestSensorEntityDescription
@property
def native_value(self) -> float | None:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.data)