hass-core/homeassistant/components/smlight/sensor.py
TimL 98a007cb2f
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675)
* Initial SMLIGHT integration

Signed-off-by: Tim Lunn <tl@smlight.tech>

* Generated content

Signed-off-by: Tim Lunn <tl@smlight.tech>

* Cleanup LOGGING

* Use runtime data

* Call super first

* coordinator instance attributes

* Move coordinatorEntity and attr to base class

* cleanup sensors

* update strings to use sentence case

* Improve reauth flow on incorrect credentials

* Use fixture for config_flow tests and test to completion

* Split uptime hndling into a new uptime sensor entity

* Drop server side events and internet callback

will bring this back with binary sensor Platform

* consolidate coordinator setup

* entity always include connections

* get_hostname tweak

* Add tests for init, coordinator and sensor

* Use custom type SmConfigEntry

* update sensor snapshot

* Drop reauth flow for later PR

* Use _async_setup for initial setup

* drop internet to be set later

* sensor fixes

* config flow re

* typing fixes

* Bump pysmlight dependency to 0.0.12

* dont trigger invalid auth message when first loading auth step

* Merge uptime sensors back into main sensor class

* clarify uptime handling

* Apply suggestions from code review

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* address review comments

* pass host as parameter to the dataCoordinator

* drop uptime sensors for a later PR

* update sensor test snapshot

* move coordinator unique_id to _async_setup

* fix CI

* Apply suggestions from code review

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* drop invalid_auth test tag

* use snapshot_platform, update fixtures

* Finish all tests with abort or create entry

* drop coordinator tests and remove hostname support

* add test for update failure on connection error

* use freezer for update_failed test

* fix pysmlight imports

---------

Signed-off-by: Tim Lunn <tl@smlight.tech>
Co-authored-by: Tim Lunn <tim@feathertop.org>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 10:44:06 +02:00

103 lines
3.1 KiB
Python

"""Support for SLZB-06 sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from pysmlight import Sensors
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import EntityCategory, UnitOfInformation, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmConfigEntry
from .coordinator import SmDataUpdateCoordinator
from .entity import SmEntity
@dataclass(frozen=True, kw_only=True)
class SmSensorEntityDescription(SensorEntityDescription):
"""Class describing SMLIGHT sensor entities."""
entity_category = EntityCategory.DIAGNOSTIC
value_fn: Callable[[Sensors], float | None]
SENSORS = [
SmSensorEntityDescription(
key="core_temperature",
translation_key="core_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
value_fn=lambda x: x.esp32_temp,
),
SmSensorEntityDescription(
key="zigbee_temperature",
translation_key="zigbee_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
value_fn=lambda x: x.zb_temp,
),
SmSensorEntityDescription(
key="ram_usage",
translation_key="ram_usage",
device_class=SensorDeviceClass.DATA_SIZE,
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
entity_registry_enabled_default=False,
value_fn=lambda x: x.ram_usage,
),
SmSensorEntityDescription(
key="fs_usage",
translation_key="fs_usage",
device_class=SensorDeviceClass.DATA_SIZE,
native_unit_of_measurement=UnitOfInformation.KILOBYTES,
entity_registry_enabled_default=False,
value_fn=lambda x: x.fs_used,
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: SmConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up SMLIGHT sensor based on a config entry."""
coordinator = entry.runtime_data
async_add_entities(
SmSensorEntity(coordinator, description) for description in SENSORS
)
class SmSensorEntity(SmEntity, SensorEntity):
"""Representation of a slzb sensor."""
entity_description: SmSensorEntityDescription
def __init__(
self,
coordinator: SmDataUpdateCoordinator,
description: SmSensorEntityDescription,
) -> None:
"""Initiate slzb sensor."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{coordinator.unique_id}_{description.key}"
@property
def native_value(self) -> float | None:
"""Return the sensor value."""
return self.entity_description.value_fn(self.coordinator.data.sensors)