From 8addaebb48b1554d7d8af730f16075917b5d71fd Mon Sep 17 00:00:00 2001 From: David Vallee Delisle Date: Sat, 15 Jan 2022 11:29:04 -0500 Subject: [PATCH] Adding back switch entities --- custom_components/hilo/switch.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 custom_components/hilo/switch.py diff --git a/custom_components/hilo/switch.py b/custom_components/hilo/switch.py new file mode 100644 index 00000000..43604179 --- /dev/null +++ b/custom_components/hilo/switch.py @@ -0,0 +1,46 @@ +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 homeassistant.util import slugify + +from . import Hilo, HiloEntity +from .const import DOMAIN, LOG, SWITCH_CLASSES + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + hilo = hass.data[DOMAIN][entry.entry_id] + entities = [] + + for d in hilo.devices.all: + if d.type in SWITCH_CLASSES: + d._entity = HiloSwitch(hilo, d) + entities.append(d._entity) + async_add_entities(entities) + + +class HiloSwitch(HiloEntity, SwitchEntity): + def __init__(self, hilo: Hilo, device): + super().__init__(hilo, device=device, name=device.name) + self._attr_unique_id = f"{slugify(device.name)}-switch" + LOG.debug(f"Setting up Switch entity: {self._attr_name}") + + @property + def state(self): + return self._device.state + + @property + def is_on(self): + return self._device.get_value("is_on") + + async def async_turn_off(self, **kwargs): + LOG.info(f"{self._device._tag} Turning off") + await self._device.set_attribute("is_on", False) + self.async_schedule_update_ha_state(True) + + async def async_turn_on(self, **kwargs): + LOG.info(f"{self._device._tag} Turning on") + await self._device.set_attribute("is_on", True) + self.async_schedule_update_ha_state(True)