|
|
发表于 2025-11-10 11:24:45
|
显示全部楼层
替换sensor.py文件内容
"""Vaillant sensors."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.const import (
UnitOfTemperature
)
from .client import VaillantClient
from .const import CONF_DID, DISPATCHERS, DOMAIN, EVT_DEVICE_CONNECTED, API_CLIENT
from .entity import VaillantEntity
_LOGGER = logging.getLogger(__name__)
SENSOR_DESCRIPTIONS = (
SensorEntityDescription(
key="water_pressure",
name="供暖水压",
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="bar",
),
SensorEntityDescription(
key="indoor_temperature",
name="室内温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Tank_temperature",
name="水箱温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Outdoor_Temperature",
name="室外温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="DHW_setpoint",
name="生活热水设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Lower_Limitation_of_CH_Setpoint",
name="暖气最小设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Upper_Limitation_of_CH_Setpoint",
name="暖气最大设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Lower_Limitation_of_DHW_Setpoint",
name="生活热水最小设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Upper_Limitation_of_DHW_Setpoint",
name="生活热水最大设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Flow_Temperature_Setpoint",
name="暖气设置温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Flow_temperature",
name="供暖出水温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="return_temperature",
name="供暖回水温度",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(
key="Mode_Setting_CH",
name="暖气模式设置",
),
SensorEntityDescription(
key="Heating_System_Setting",
name="供暖系统设置",
),
SensorEntityDescription(
key="burn_status",
name="燃烧器状态",
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> bool:
"""Set up Vaillant sensors."""
device_id = entry.data.get(CONF_DID)
client: VaillantClient = hass.data[DOMAIN][API_CLIENT][
entry.entry_id
]
added_entities = []
@callback
def async_new_entities(device_attrs: dict[str, Any]):
_LOGGER.debug("add vaillant sensor entities. device attrs: %s", device_attrs)
new_entities = []
for description in SENSOR_DESCRIPTIONS:
if (
description.key in device_attrs
and description.key not in added_entities
):
new_entities.append(VaillantSensorEntity(client, description))
added_entities.append(description.key)
if len(new_entities) > 0:
async_add_entities(new_entities)
unsub = async_dispatcher_connect(
hass, EVT_DEVICE_CONNECTED.format(device_id), async_new_entities
)
hass.data[DOMAIN][DISPATCHERS][device_id].append(unsub)
return True
class VaillantSensorEntity(VaillantEntity, SensorEntity):
"""Define a Vaillant sensor entity."""
def __init__(
self,
client: VaillantClient,
description: SensorEntityDescription,
):
super().__init__(client)
self.entity_description = description
@property
def unique_id(self) -> str | None:
"""Return a unique ID."""
return f"{self.device.id}_{self.entity_description.key}"
@callback
def update_from_latest_data(self, data: dict[str, Any]) -> None:
"""Update the entity from the latest data."""
if(self.entity_description.key in data):
value = data.get(self.entity_description.key)
self._attr_native_value = value
self._attr_available = value is not None
self.async_schedule_update_ha_state(True) |
|