core/homeassistant/components/ecoforest/sensor.py

116 lines
3.6 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 Alarm, Device, State
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
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 homeassistant.helpers.typing import StateType
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
from .const import DOMAIN
from .coordinator import EcoforestCoordinator
from .entity import EcoforestEntity
_LOGGER = logging.getLogger(__name__)
STATUS_TYPE = [s.value for s in State]
ALARM_TYPE = [a.value for a in Alarm] + ["none"]
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
@dataclass
class EcoforestRequiredKeysMixin:
"""Mixin for required keys."""
value_fn: Callable[[Device], StateType]
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
@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,
),
EcoforestSensorEntityDescription(
key="cpu_temperature",
translation_key="cpu_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
value_fn=lambda data: data.cpu_temperature,
),
EcoforestSensorEntityDescription(
key="gas_temperature",
translation_key="gas_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
value_fn=lambda data: data.gas_temperature,
),
EcoforestSensorEntityDescription(
key="ntc_temperature",
translation_key="ntc_temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
value_fn=lambda data: data.ntc_temperature,
),
EcoforestSensorEntityDescription(
key="status",
translation_key="status",
device_class=SensorDeviceClass.ENUM,
options=STATUS_TYPE,
value_fn=lambda data: data.state.value,
),
EcoforestSensorEntityDescription(
key="alarm",
translation_key="alarm",
device_class=SensorDeviceClass.ENUM,
options=ALARM_TYPE,
icon="mdi:alert",
value_fn=lambda data: data.alarm.value if data.alarm else "none",
),
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
)
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) -> StateType:
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
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.data)