mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Add sensor entity to Zimi integration (#144329)
* Import sensor.py * Light design alignment * Fix merge error * Refactor with extend * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * value_fn and inline refactoring * strings.json and translation_key * Add sensor_name * Revert "Add sensor_name" This reverts commit ad3da048e9c5a6ecdb15052c253de7dc46b1120f. * Default naming for sensors * Remove uneeded 'garage' and use default battery name * Bump to zcc-helper 3.5.2 which maps "Garage Controller" tp "Garage" in device.name * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update strings.json * Revert "Bump to zcc-helper 3.5.2 which maps "Garage Controller" tp "Garage" in device.name" This reverts commit 345ef8a4859c8d0e188462c9a69a4fab8f284b69. * Update homeassistant/components/zimi/sensor.py --------- Co-authored-by: Josef Zweck <josef@zweck.dev>
This commit is contained in:
parent
7c306acd5d
commit
fd09476b28
@ -16,7 +16,7 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .helpers import async_connect_to_controller
|
from .helpers import async_connect_to_controller
|
||||||
|
|
||||||
PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SWITCH]
|
PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -21,7 +21,9 @@ class ZimiEntity(Entity):
|
|||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(self, device: ControlPointDevice, api: ControlPoint) -> None:
|
def __init__(
|
||||||
|
self, device: ControlPointDevice, api: ControlPoint, use_device_name=True
|
||||||
|
) -> None:
|
||||||
"""Initialize an HA Entity which is a ZimiDevice."""
|
"""Initialize an HA Entity which is a ZimiDevice."""
|
||||||
|
|
||||||
self._device = device
|
self._device = device
|
||||||
@ -36,7 +38,8 @@ class ZimiEntity(Entity):
|
|||||||
suggested_area=device.room,
|
suggested_area=device.room,
|
||||||
via_device=(DOMAIN, api.mac),
|
via_device=(DOMAIN, api.mac),
|
||||||
)
|
)
|
||||||
self._attr_name = device.name.strip()
|
if use_device_name:
|
||||||
|
self._attr_name = device.name.strip()
|
||||||
self._attr_suggested_area = device.room
|
self._attr_suggested_area = device.room
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
103
homeassistant/components/zimi/sensor.py
Normal file
103
homeassistant/components/zimi/sensor.py
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
"""Platform for sensor integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from zcc import ControlPoint
|
||||||
|
from zcc.device import ControlPointDevice
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import (
|
||||||
|
SensorDeviceClass,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
|
from . import ZimiConfigEntry
|
||||||
|
from .entity import ZimiEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class ZimiSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""Class describing Zimi sensor entities."""
|
||||||
|
|
||||||
|
value_fn: Callable[[ControlPointDevice], StateType]
|
||||||
|
|
||||||
|
|
||||||
|
GARAGE_SENSOR_DESCRIPTIONS: tuple[ZimiSensorEntityDescription, ...] = (
|
||||||
|
ZimiSensorEntityDescription(
|
||||||
|
key="door_temperature",
|
||||||
|
translation_key="door_temperature",
|
||||||
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
value_fn=lambda device: device.door_temp,
|
||||||
|
),
|
||||||
|
ZimiSensorEntityDescription(
|
||||||
|
key="garage_battery",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
device_class=SensorDeviceClass.BATTERY,
|
||||||
|
value_fn=lambda device: device.battery_level,
|
||||||
|
),
|
||||||
|
ZimiSensorEntityDescription(
|
||||||
|
key="garage_temperature",
|
||||||
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
value_fn=lambda device: device.garage_temp,
|
||||||
|
),
|
||||||
|
ZimiSensorEntityDescription(
|
||||||
|
key="garage_humidty",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
device_class=SensorDeviceClass.HUMIDITY,
|
||||||
|
value_fn=lambda device: device.garage_humidity,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ZimiConfigEntry,
|
||||||
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Zimi Sensor platform."""
|
||||||
|
|
||||||
|
api = config_entry.runtime_data
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
ZimiSensor(device, description, api)
|
||||||
|
for device in api.sensors
|
||||||
|
for description in GARAGE_SENSOR_DESCRIPTIONS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ZimiSensor(ZimiEntity, SensorEntity):
|
||||||
|
"""Representation of a Zimi sensor."""
|
||||||
|
|
||||||
|
entity_description: ZimiSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: ControlPointDevice,
|
||||||
|
description: ZimiSensorEntityDescription,
|
||||||
|
api: ControlPoint,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize an ZimiSensor with specified type."""
|
||||||
|
|
||||||
|
super().__init__(device, api, use_device_name=False)
|
||||||
|
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = device.identifier + "." + self.entity_description.key
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> StateType:
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
|
||||||
|
return self.entity_description.value_fn(self._device)
|
@ -42,5 +42,12 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"entity": {
|
||||||
|
"sensor": {
|
||||||
|
"door_temperature": {
|
||||||
|
"name": "Outside temperature"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user