Files
fgl-aircon/custom_components/fglair/sensor.py
T
petr.polezhaev 3e0b6b3653 ha(H3): превью шаблона с конверсиями; climate и сущности
- config flow: шаг preview (шаблон A/B/F, диапазон/шаг уставки, линейные
  коэффициенты num/den/offset, текст-превью по данным trial: hvac/fan/
  swing/capabilities/текущая температура)
- coordinator: push-driven DataUpdateCoordinator (снимок кэша, доступность
  online/recovering, первичный GET-батч, один batch_commit на действие)
- overrides.py: ConversionSet (ручные линейные конверсии и диапазон поверх
  таблиц ядра, применяются в HA)
- entity.py: DeviceInfo/unique_id/доступность, capabilities-фильтры
- climate: hvac/fan/swing/vertical+horizontal swing/preset ECO-BOOST,
  уставка 16-30 °C (шаг по шаблону), current_temperature
- sensor: room_temperature, error_code, connection_state (диагностика)
- binary_sensor: connectivity + флаги op_status (PROTOCOL §8.4)
- switch: 8 флагов-свойств; select: положения заслонок 0..N-1
- тесты: превью/конверсии/ошибки диапазона, сущности против mock_ac
  (снимок, команды, capabilities-фильтр, ручная конверсия), ConversionSet
- mock_ac: --set NAME=VALUE (0b/0x) для сценариев
- план: demand_control отложен (семантика не зафиксирована)
2026-09-29 12:44:34 +03:00

79 lines
2.7 KiB
Python

"""Sensors: температура помещения, код ошибки, состояние связи."""
from __future__ import annotations
from typing import Optional
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.const import EntityCategory, UnitOfTemperature
from pyfglair import Prop, State
from pyfglair.templates import template_info
from .coordinator import FglairCoordinator, FglairRuntime
from .entity import FglairEntity
class FglairRoomTemperature(FglairEntity, SensorEntity):
_attr_translation_key = "room_temperature"
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
_attr_suggested_display_precision = 1
def __init__(self, coordinator: FglairCoordinator) -> None:
super().__init__(coordinator, "display_temperature", "room_temperature")
@property
def native_value(self) -> Optional[float]:
display = self.display_value(Prop.DISPLAY_TEMPERATURE)
return display / 10 if display is not None else None
class FglairErrorCode(FglairEntity, SensorEntity):
_attr_translation_key = "error_code"
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(self, coordinator: FglairCoordinator) -> None:
super().__init__(coordinator, "error_code", "error_code")
@property
def native_value(self) -> Optional[int]:
return self.raw_value(Prop.ERROR_CODE)
class FglairConnectionState(FglairEntity, SensorEntity):
_attr_translation_key = "connection_state"
_attr_device_class = SensorDeviceClass.ENUM
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_options = [state.name.lower() for state in State]
def __init__(self, coordinator: FglairCoordinator) -> None:
super().__init__(coordinator, "connection_state", "connection_state")
@property
def available(self) -> bool:
# Диагностика связи должна быть видна и при offline.
return True
@property
def native_value(self) -> str:
return self.coordinator.data.state.name.lower()
async def async_setup_entry(hass, entry, async_add_entities) -> None:
runtime: FglairRuntime = entry.runtime_data
coordinator = runtime.coordinator
template = runtime.client.template
props = {info.prop for info in template_info(template)}
entities = [FglairConnectionState(coordinator)]
if Prop.DISPLAY_TEMPERATURE in props:
entities.append(FglairRoomTemperature(coordinator))
if Prop.ERROR_CODE in props:
entities.append(FglairErrorCode(coordinator))
async_add_entities(entities)