From 53d1f1f9ae2d69ae414358a830dc8bfc02517209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sat, 15 Oct 2022 23:12:58 +0200 Subject: [PATCH] Add switch component (untested) --- custom_components/plejd/__init__.py | 2 +- custom_components/plejd/switch.py | 81 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 custom_components/plejd/switch.py diff --git a/custom_components/plejd/__init__.py b/custom_components/plejd/__init__.py index 3c37405..21d3cf0 100644 --- a/custom_components/plejd/__init__.py +++ b/custom_components/plejd/__init__.py @@ -51,7 +51,7 @@ async def async_setup_entry(hass, config_entry): } ) - await hass.config_entries.async_forward_entry_setups(config_entry, ["light"]) + await hass.config_entries.async_forward_entry_setups(config_entry, ["light", "switch"]) async def _ping(now=None): if hass.data[DOMAIN]["stopping"]: return diff --git a/custom_components/plejd/switch.py b/custom_components/plejd/switch.py new file mode 100644 index 0000000..f3aab28 --- /dev/null +++ b/custom_components/plejd/switch.py @@ -0,0 +1,81 @@ +from builtins import property +import logging +from homeassistant.components.switch import SwitchEntity +from homeassistant.helpers.update_coordinator import CoordinatorEntity, DataUpdateCoordinator + + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "plejd" + +async def async_setup_entry(hass, config_entry, async_add_entities): + devices = hass.data[DOMAIN]["devices"] + + entities = [] + for d in devices: + dev = devices[d] + if dev.type == "switch": + coordinator = Coordinator(hass, dev) + async def updateCallback(data): + coordinator.async_set_updated_data(data) + dev.updateCallback = updateCallback + light = PlejdSwitch(coordinator, dev) + entities.append(light) + async_add_entities(entities, False) + +class Coordinator(DataUpdateCoordinator): + def __init__(self, hass, device): + super().__init__(hass, _LOGGER, name="Plejd Coordinator") + self.device = device + +class PlejdSwitch(SwitchEntity, CoordinatorEntity): + def __init__(self, coordinator, device): + CoordinatorEntity.__init__(self, coordinator) + SwitchEntity.__init__(self) + self.device = device + + @property + def _data(self): + return self.coordinator.data or {} + + @property + def available(self): + return self._data.get("state", None) is not None + + @property + def device_info(self): + return { + "identifiers": {(DOMAIN, self.device.BLE_address)}, + "name": self.device.name, + "manufacturer": "Plejd", + "model": self.device.model, + #"connections": ???, + "suggested_area": self.device.room, + "sw_version": self.device.firmware, + } + + @property + def has_entity_name(self): + return True + + @property + def name(self): + return None + + @property + def unique_id(self): + return self.device.BLE_address + + @property + def is_on(self): + return self._data.get("state") + + async def async_turn_on(self, **_): + await self.device.turn_on(None) + pass + + async def async_turn_off(self, **_): + await self.device.turn_off() + pass + +