From 3bc4357974ab924ecdb54a1a7d688e539b982ee5 Mon Sep 17 00:00:00 2001 From: Vladimir Ermakov Date: Sun, 25 Feb 2024 14:22:10 +0100 Subject: [PATCH] switch: add basic switch to trigger security alarm mode --- custom_components/myheat/const.py | 7 ++++- custom_components/myheat/switch.py | 48 ++++++++++++++---------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/custom_components/myheat/const.py b/custom_components/myheat/const.py index 03596a4..14312b9 100644 --- a/custom_components/myheat/const.py +++ b/custom_components/myheat/const.py @@ -26,7 +26,12 @@ SENSOR = "sensor" SWITCH = "switch" -PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.BINARY_SENSOR] +PLATFORMS = [ + Platform.CLIMATE, + Platform.SENSOR, + Platform.BINARY_SENSOR, + Platform.SWITCH, +] # Defaults DEFAULT_NAME = DOMAIN diff --git a/custom_components/myheat/switch.py b/custom_components/myheat/switch.py index 69b0bcb..602acfc 100644 --- a/custom_components/myheat/switch.py +++ b/custom_components/myheat/switch.py @@ -1,44 +1,42 @@ """Switch platform for MyHeat.""" from homeassistant.components.switch import SwitchEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DEFAULT_NAME from .const import DOMAIN -from .const import ICON -from .const import SWITCH from .entity import MhEntity -async def async_setup_entry(hass, entry, async_add_devices): +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_devices: AddEntitiesCallback, +): """Setup sensor platform.""" coordinator = hass.data[DOMAIN][entry.entry_id] - async_add_devices([MhBinarySwitch(coordinator, entry)]) + async_add_devices([MhSecuritySwitch(coordinator, entry)]) -class MhBinarySwitch(MhEntity, SwitchEntity): +class MhSecuritySwitch(MhEntity, SwitchEntity): """myheat switch class.""" - async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument - """Turn on the switch.""" - await self.coordinator.api.async_set_title("bar") - await self.coordinator.async_request_refresh() - - async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument - """Turn off the switch.""" - await self.coordinator.api.async_set_title("foo") - await self.coordinator.async_request_refresh() + _attr_icon = "mdi:security" @property - def name(self): - """Return the name of the switch.""" - return f"{DEFAULT_NAME}_{SWITCH}" + def name(self) -> str: + return f"{self._mh_name} security alarm" @property - def icon(self): - """Return the icon of this switch.""" - return ICON + def unique_id(self) -> str: + return f"{super().unique_id}security" - @property - def is_on(self): - """Return true if the switch is on.""" - return self.coordinator.data.get("title", "") == "foo" + async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument + await self.coordinator.api.async_set_security_mode(mode=True) + # NOTE: we cannot query the state, so we have to make some assumptions + self._attr_is_on = True + + async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument + await self.coordinator.api.async_set_security_mode(mode=False) + self._attr_is_on = False