From 0251b07e2f6a59e89e22194e5687abf2f56ad24c Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 12 Nov 2024 20:19:57 +0200 Subject: [PATCH] feat: home/away switch --- custom_components/ouman_eh800/__init__.py | 8 ++- custom_components/ouman_eh800/eh800.py | 5 ++ custom_components/ouman_eh800/switch.py | 74 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 custom_components/ouman_eh800/switch.py diff --git a/custom_components/ouman_eh800/__init__.py b/custom_components/ouman_eh800/__init__.py index 89ef93b..8c584ca 100644 --- a/custom_components/ouman_eh800/__init__.py +++ b/custom_components/ouman_eh800/__init__.py @@ -12,7 +12,13 @@ from .eh800 import EH800 _LOGGER = logging.getLogger(__name__) -PLATFORMS = [Platform.CLIMATE, Platform.NUMBER, Platform.SENSOR, Platform.VALVE] +PLATFORMS = [ + Platform.CLIMATE, + Platform.NUMBER, + Platform.SENSOR, + Platform.SWITCH, + Platform.VALVE, +] UPDATE_INTERVAL = timedelta(minutes=1) diff --git a/custom_components/ouman_eh800/eh800.py b/custom_components/ouman_eh800/eh800.py index 7b86128..fdda8c1 100644 --- a/custom_components/ouman_eh800/eh800.py +++ b/custom_components/ouman_eh800/eh800.py @@ -49,6 +49,11 @@ VALUES: tuple[Value, ...] = ( Value(key="l1_tmrsp", register="S_274_85"), # Mittaukset > L1 Venttiilin asento Value(key="l1_valve_position", register="S_272_85"), + # EH-800 > Kotona/Poissa + # 0 = Kotona + # 1 = Poissa + # 2 = Ei K/P-ohjausta + Value(key="home_away", register="S_135_85"), # EH-800 > Huonelämpötila Value(key="room_temperature", register="S_261_85"), # EH-800 > Huonelämpötilan hienosäätö diff --git a/custom_components/ouman_eh800/switch.py b/custom_components/ouman_eh800/switch.py new file mode 100644 index 0000000..6bc4d02 --- /dev/null +++ b/custom_components/ouman_eh800/switch.py @@ -0,0 +1,74 @@ +import logging + +from homeassistant.components.switch import ( + SwitchDeviceClass, + SwitchEntity, + SwitchEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import OumanEH800Device +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up Ouman EH-800 device sensors.""" + device = hass.data[DOMAIN].get(entry.entry_id) + + entities: list[OumanEH800DeviceHomeAwaySwitch] = [ + OumanEH800DeviceHomeAwaySwitch( + device, + SwitchEntityDescription( + key="home_away", + device_class=SwitchDeviceClass.SWITCH, + ), + ) + ] + + async_add_entities(entities, True) + + +class OumanEH800DeviceHomeAwaySwitch(SwitchEntity): + entity_description: SwitchEntityDescription + + def __init__( + self, + device: OumanEH800Device, + description: SwitchEntityDescription, + ) -> None: + self._device = device + self.entity_description = description + + self._attr_name = description.key.replace("_", " ").capitalize() + self._attr_unique_id = f"ouman_eh800_{description.key}" + self._attr_device_info = device.device_info + + @property + def is_on(self) -> bool: + value = int(self._device.device.data.get(self.entity_description.key)) + if value > 0: + return False + return True + + async def async_turn_off( + self, + **kwargs, # pylint: disable=unused-argument + ): + await self._device.device.update_value(self.entity_description.key, 1) + self.async_write_ha_state() + + async def async_turn_on( + self, + **kwargs, # pylint: disable=unused-argument + ): + await self._device.device.update_value(self.entity_description.key, 0) + self.async_write_ha_state() + + async def async_update(self) -> None: + await self._device.async_update() -- 2.44.1