This commit is contained in:
Sergey-tbot 2025-07-16 18:01:03 +00:00 committed by GitHub
commit 106531ee06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 146 additions and 78 deletions

View file

@ -1,12 +1,15 @@
import logging import logging
from datetime import timedelta
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import voluptuous as vol # type: ignore[import-untyped] import voluptuous as vol # type: ignore[import-untyped]
from homeassistant.core import HomeAssistant, callback
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers import config_validation as cv, aiohttp_client from homeassistant.helpers import config_validation as cv, aiohttp_client
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from pyhon import Hon from pyhon import Hon
@ -27,47 +30,114 @@ CONFIG_SCHEMA = vol.Schema(
) )
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Hon from a config entry."""
session = aiohttp_client.async_get_clientsession(hass) session = aiohttp_client.async_get_clientsession(hass)
if (config_dir := hass.config.config_dir) is None:
raise ValueError("Missing Config Dir") try:
hon = await Hon( # Initialize Hon instance in executor
email=entry.data[CONF_EMAIL], def init_hon():
password=entry.data[CONF_PASSWORD], """Initialize Hon instance."""
mobile_id=MOBILE_ID, return Hon(
session=session, email=entry.data[CONF_EMAIL],
test_data_path=Path(config_dir), password=entry.data[CONF_PASSWORD],
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""), mobile_id=MOBILE_ID,
).create() session=session,
test_data_path=Path(hass.config.config_dir),
refresh_token=entry.data.get(CONF_REFRESH_TOKEN, ""),
)
# Create Hon instance in executor
hon = await hass.async_add_executor_job(init_hon)
# Create and initialize
hon = await hon.create()
except Exception as exc:
_LOGGER.error("Error creating Hon instance: %s", exc)
raise
async def async_update_data() -> dict[str, Any]:
"""Fetch data from API."""
try:
for appliance in hon.appliances:
await appliance.update()
return {"last_update": hon.api.auth.refresh_token}
except Exception as exc:
_LOGGER.error("Error updating Hon data: %s", exc)
raise
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=DOMAIN,
update_method=async_update_data,
update_interval=timedelta(seconds=60),
)
def _handle_mqtt_update(_: Any) -> None:
"""Handle MQTT updates."""
try:
coordinator.async_set_updated_data({"last_update": hon.api.auth.refresh_token})
except Exception as exc:
_LOGGER.error("Error handling MQTT update: %s", exc)
def handle_update(msg: Any) -> None:
"""Handle updates from MQTT subscription in a thread-safe way."""
try:
hass.loop.call_soon_threadsafe(_handle_mqtt_update, msg)
except Exception as exc:
_LOGGER.error("Error scheduling MQTT update: %s", exc)
# Subscribe to MQTT updates with error handling
try:
hon.subscribe_updates(handle_update)
except Exception as exc:
_LOGGER.error("Error subscribing to MQTT updates: %s", exc)
# Initial data fetch
try:
await coordinator.async_config_entry_first_refresh()
except Exception as exc:
_LOGGER.error("Error during initial refresh: %s", exc)
raise
# Save the new refresh token # Save the new refresh token
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token} entry, data={**entry.data, CONF_REFRESH_TOKEN: hon.api.auth.refresh_token}
) )
coordinator: DataUpdateCoordinator[dict[str, Any]] = DataUpdateCoordinator(
hass, _LOGGER, name=DOMAIN
)
hon.subscribe_updates(coordinator.async_set_updated_data)
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = {"hon": hon, "coordinator": coordinator} hass.data[DOMAIN][entry.unique_id] = {"hon": hon, "coordinator": coordinator}
for platform in PLATFORMS: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
return True return True
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
refresh_token = hass.data[DOMAIN][entry.unique_id]["hon"].api.auth.refresh_token """Unload a config entry."""
try:
hon = hass.data[DOMAIN][entry.unique_id]["hon"]
hass.config_entries.async_update_entry( # Store refresh token
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token} refresh_token = hon.api.auth.refresh_token
)
unload = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) # Unsubscribe from updates
if unload: try:
if not hass.data[DOMAIN]: hon.subscribe_updates(None) # Remove subscription
hass.data.pop(DOMAIN, None) except Exception as exc:
return unload _LOGGER.warning("Error unsubscribing from updates: %s", exc)
# Update entry with latest refresh token
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
)
# Unload platforms
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.unique_id)
return unload_ok
except Exception as exc:
_LOGGER.error("Error unloading entry: %s", exc)
return False

View file

@ -7,9 +7,8 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from .const import DOMAIN from .const import DOMAIN
from .entity import HonEntity from .entity import HonEntity
@ -317,7 +316,7 @@ BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD"
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:

View file

@ -6,7 +6,7 @@ from homeassistant.components.button import ButtonEntityDescription, ButtonEntit
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.core import HomeAssistant
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from .const import DOMAIN from .const import DOMAIN
@ -56,7 +56,7 @@ BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities: list[HonButtonType] = [] entities: list[HonButtonType] = []
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:
@ -88,7 +88,7 @@ class HonButtonEntity(HonEntity, ButtonEntity):
class HonDeviceInfo(HonEntity, ButtonEntity): class HonDeviceInfo(HonEntity, ButtonEntity):
def __init__( def __init__(
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance self, hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance
) -> None: ) -> None:
super().__init__(hass, entry, device) super().__init__(hass, entry, device)
@ -108,7 +108,7 @@ class HonDeviceInfo(HonEntity, ButtonEntity):
class HonDataArchive(HonEntity, ButtonEntity): class HonDataArchive(HonEntity, ButtonEntity):
def __init__( def __init__(
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance self, hass: HomeAssistant, entry: ConfigEntry, device: HonAppliance
) -> None: ) -> None:
super().__init__(hass, entry, device) super().__init__(hass, entry, device)

View file

@ -19,9 +19,8 @@ from homeassistant.const import (
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
@ -104,7 +103,7 @@ CLIMATES: dict[
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
entity: HonClimateEntity | HonACClimateEntity entity: HonClimateEntity | HonACClimateEntity
@ -130,7 +129,7 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: HonACClimateEntityDescription, description: HonACClimateEntityDescription,
@ -199,7 +198,7 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
self._attr_hvac_mode = hvac_mode self._attr_hvac_mode = hvac_mode
if hvac_mode == HVACMode.OFF: if hvac_mode == HVACMode.OFF:
await self._device.commands["stopProgram"].send() await self._device.commands["stopProgram"].send()
self._device.sync_command("stopProgram", "settings") self._device.settings["settings.onOffStatus"].value = "2"
else: else:
self._device.settings["settings.onOffStatus"].value = "1" self._device.settings["settings.onOffStatus"].value = "1"
setting = self._device.settings["settings.machMode"] setting = self._device.settings["settings.machMode"]
@ -217,6 +216,10 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
self._device.sync_command("startProgram", "settings") self._device.sync_command("startProgram", "settings")
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
fix_param = self._device.commands["stopProgram"].parameters.get("windDirectionVerticalPositionSequence")
if fix_param and fix_param.value == "0":
fix_param.value = "2"
_LOGGER.warning("🔧 Patched 'windDirectionVerticalPositionSequence' from '0' to '2'")
await self._device.commands["stopProgram"].send() await self._device.commands["stopProgram"].send()
self._device.sync_command("stopProgram", "settings") self._device.sync_command("stopProgram", "settings")
@ -282,7 +285,7 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
if swing_mode in [SWING_OFF, SWING_HORIZONTAL] and vertical.value == "8": if swing_mode in [SWING_OFF, SWING_HORIZONTAL] and vertical.value == "8":
vertical.value = "5" vertical.value = "5"
if swing_mode in [SWING_OFF, SWING_VERTICAL] and horizontal.value == "7": if swing_mode in [SWING_OFF, SWING_VERTICAL] and horizontal.value == "7":
horizontal.value = "0" horizontal.value = "2"
self._attr_swing_mode = swing_mode self._attr_swing_mode = swing_mode
await self._device.commands["settings"].send() await self._device.commands["settings"].send()
self.async_write_ha_state() self.async_write_ha_state()
@ -299,7 +302,7 @@ class HonClimateEntity(HonEntity, ClimateEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: HonClimateEntityDescription, description: HonClimateEntityDescription,

View file

@ -1,9 +1,8 @@
from typing import Optional, Any from typing import Optional, Any
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
) )
@ -20,7 +19,7 @@ class HonEntity(CoordinatorEntity[DataUpdateCoordinator[dict[str, Any]]]):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: Optional[HonEntityDescription] = None, description: Optional[HonEntityDescription] = None,

View file

@ -8,9 +8,8 @@ from homeassistant.components.fan import (
FanEntityFeature, FanEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util.percentage import ( from homeassistant.util.percentage import (
percentage_to_ranged_value, percentage_to_ranged_value,
ranged_value_to_percentage, ranged_value_to_percentage,
@ -36,7 +35,7 @@ FANS: dict[str, tuple[FanEntityDescription, ...]] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:
@ -56,7 +55,7 @@ class HonFanEntity(HonEntity, FanEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: FanEntityDescription, description: FanEntityDescription,

View file

@ -8,9 +8,8 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
@ -53,7 +52,7 @@ LIGHTS: dict[str, tuple[LightEntityDescription, ...]] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:
@ -73,7 +72,7 @@ class HonLightEntity(HonEntity, LightEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: LightEntityDescription, description: LightEntityDescription,

View file

@ -3,9 +3,8 @@ from typing import Any
from homeassistant.components.lock import LockEntity, LockEntityDescription from homeassistant.components.lock import LockEntity, LockEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from pyhon.parameter.base import HonParameter from pyhon.parameter.base import HonParameter
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
@ -26,7 +25,7 @@ LOCKS: dict[str, tuple[LockEntityDescription, ...]] = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances: for device in hass.data[DOMAIN][entry.unique_id]["hon"].appliances:

View file

@ -2,7 +2,8 @@
"domain": "hon", "domain": "hon",
"name": "Haier hOn", "name": "Haier hOn",
"codeowners": [ "codeowners": [
"@Andre0512" "@Andre0512",
"@galvani"
], ],
"config_flow": true, "config_flow": true,
"documentation": "https://github.com/Andre0512/hon/", "documentation": "https://github.com/Andre0512/hon/",
@ -11,5 +12,5 @@
"requirements": [ "requirements": [
"pyhOn==0.17.5" "pyhOn==0.17.5"
], ],
"version": "0.14.0" "version": "0.14.1"
} }

View file

@ -8,10 +8,9 @@ from homeassistant.components.number import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTime, UnitOfTemperature from homeassistant.const import UnitOfTime, UnitOfTemperature
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
@ -207,7 +206,7 @@ NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"])
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
entity: HonNumberEntity | HonConfigNumberEntity entity: HonNumberEntity | HonConfigNumberEntity
@ -230,7 +229,7 @@ class HonNumberEntity(HonEntity, NumberEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: HonNumberEntityDescription, description: HonNumberEntityDescription,
@ -285,7 +284,7 @@ class HonConfigNumberEntity(HonEntity, NumberEntity):
def __init__( def __init__(
self, self,
hass: HomeAssistantType, hass: HomeAssistant,
entry: ConfigEntry, entry: ConfigEntry,
device: HonAppliance, device: HonAppliance,
description: HonConfigNumberEntityDescription, description: HonConfigNumberEntityDescription,

View file

@ -6,10 +6,9 @@ from dataclasses import dataclass
from homeassistant.components.select import SelectEntity, SelectEntityDescription from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from . import const from . import const
from .const import DOMAIN from .const import DOMAIN
@ -211,7 +210,7 @@ SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"])
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
entity: HonSelectEntity | HonConfigSelectEntity entity: HonSelectEntity | HonConfigSelectEntity

View file

@ -21,10 +21,9 @@ from homeassistant.const import (
UnitOfTime, UnitOfTime,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from . import const from . import const
from .const import DOMAIN from .const import DOMAIN
@ -808,7 +807,7 @@ SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"])
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
entity: HonSensorEntity | HonConfigSensorEntity entity: HonSensorEntity | HonConfigSensorEntity
@ -834,6 +833,7 @@ class HonSensorEntity(HonEntity, SensorEntity):
@callback @callback
def _handle_coordinator_update(self, update: bool = True) -> None: def _handle_coordinator_update(self, update: bool = True) -> None:
"""Handle updated data from the coordinator."""
value = self._device.get(self.entity_description.key, "") value = self._device.get(self.entity_description.key, "")
if self.entity_description.key == "programName": if self.entity_description.key == "programName":
if not (options := self._device.settings.get("startProgram.program")): if not (options := self._device.settings.get("startProgram.program")):
@ -844,7 +844,8 @@ class HonSensorEntity(HonEntity, SensorEntity):
value = str(get_readable(self.entity_description, value)) value = str(get_readable(self.entity_description, value))
if not value and self.entity_description.state_class is not None: if not value and self.entity_description.state_class is not None:
self._attr_native_value = 0 self._attr_native_value = 0
self._attr_native_value = value else:
self._attr_native_value = value
if update: if update:
self.async_write_ha_state() self.async_write_ha_state()
@ -854,6 +855,7 @@ class HonConfigSensorEntity(HonEntity, SensorEntity):
@callback @callback
def _handle_coordinator_update(self, update: bool = True) -> None: def _handle_coordinator_update(self, update: bool = True) -> None:
"""Handle updated data from the coordinator."""
sensor = self._device.settings.get(self.entity_description.key, None) sensor = self._device.settings.get(self.entity_description.key, None)
value: float | str value: float | str
if self.entity_description.state_class is not None: if self.entity_description.state_class is not None:
@ -874,4 +876,4 @@ class HonConfigSensorEntity(HonEntity, SensorEntity):
value = get_readable(self.entity_description, value) value = get_readable(self.entity_description, value)
self._attr_native_value = value self._attr_native_value = value
if update: if update:
self.async_write_ha_state() self.async_write_ha_state()

View file

@ -5,10 +5,9 @@ from typing import Any
from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import HomeAssistantType
from pyhon.parameter.base import HonParameter from pyhon.parameter.base import HonParameter
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
@ -403,7 +402,7 @@ SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"])
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
entities = [] entities = []
entity: HonConfigSwitchEntity | HonControlSwitchEntity | HonSwitchEntity entity: HonConfigSwitchEntity | HonControlSwitchEntity | HonSwitchEntity