Skip to content

Commit

Permalink
Add window switch
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed Dec 3, 2024
1 parent a21b0f5 commit f36faa7
Showing 1 changed file with 47 additions and 71 deletions.
118 changes: 47 additions & 71 deletions custom_components/heatzy/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,46 @@

from __future__ import annotations

from dataclasses import dataclass
import logging
from typing import Final

from heatzypy.exception import HeatzyException

from homeassistant.components.switch import SwitchEntity
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import HeatzyConfigEntry, HeatzyDataUpdateCoordinator
from .const import CONF_ATTRS, CONF_LOCK, CONF_WINDOW, DOMAIN

from .const import CONF_ATTRS, CONF_LOCK, CONF_WINDOW
from .entity import HeatzyEntity


@dataclass(frozen=True)
class HeatzySwitchEntityDescription(SwitchEntityDescription):
"""Represents an Flow Sensor."""

attr: str | None = None


SWITCH_TYPES: Final[tuple[HeatzySwitchEntityDescription, ...]] = (
HeatzySwitchEntityDescription(
key="lock",
name="Lock",
translation_key="lock",
attr=CONF_LOCK,
entity_category=EntityCategory.CONFIG,
),
HeatzySwitchEntityDescription(
key="window",
name="Window",
icon="mdi:window-open-variant",
translation_key="window",
attr=CONF_WINDOW,
entity_category=EntityCategory.CONFIG,
),
)
_LOGGER = logging.getLogger(__name__)


Expand All @@ -25,102 +52,51 @@ async def async_setup_entry(
) -> None:
"""Set the sensor platform."""
coordinator = entry.runtime_data
entities: list[LockSwitchEntity | WindowSwitchEntity] = []
entities = []

for unique_id, device in coordinator.data.items():
if device.get(CONF_ATTRS, {}).get(CONF_LOCK) is not None:
entities.append(LockSwitchEntity(coordinator, unique_id))
if device.get(CONF_ATTRS, {}).get(CONF_WINDOW) is not None:
entities.append(WindowSwitchEntity(coordinator, unique_id))
for description in SWITCH_TYPES:
if device.get(CONF_ATTRS, {}).get(description.attr) is not None:
entities.extend([SwitchEntity(coordinator, description, unique_id)])
async_add_entities(entities)


class LockSwitchEntity(CoordinatorEntity[HeatzyDataUpdateCoordinator], SwitchEntity):
"""Lock Switch."""
class SwitchEntity(HeatzyEntity, SwitchEntity):
"""Switch."""

entity_category = EntityCategory.CONFIG
_attr_has_entity_name = True
_attr_name = None

def __init__(
self, coordinator: HeatzyDataUpdateCoordinator, unique_id: str
self, coordinator: HeatzyDataUpdateCoordinator, description, did: str
) -> None:
"""Initialize switch."""
super().__init__(coordinator)
self._attr_unique_id = unique_id
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, unique_id)})
self._attr = coordinator.data[unique_id].get(CONF_ATTRS, {})
super().__init__(coordinator, description, did)

@property
def is_on(self) -> bool:
"""Return true if switch is on."""
return self._attr.get(CONF_LOCK)

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr = self.coordinator.data[self.unique_id].get(CONF_ATTRS, {})
self.async_write_ha_state()
return self._attrs.get(self.entity_description.attr) == 1

async def async_turn_on(self) -> None:
"""Turn the entity on."""
try:
await self.coordinator.api.websocket.async_control_device(
self.unique_id, {CONF_ATTRS: {CONF_LOCK: 1}}
self.unique_id, {CONF_ATTRS: {self.entity_description.attr: 1}}
)
except HeatzyException as error:
_LOGGER.error("Error to lock pilot : %s", error)
_LOGGER.error("Error to action : %s", error)

async def async_turn_off(self) -> None:
"""Turn the entity off."""
try:
await self.coordinator.api.websocket.async_control_device(
self.unique_id, {CONF_ATTRS: {CONF_LOCK: 0}}
self.unique_id, {CONF_ATTRS: {self.entity_description.attr: 0}}
)
except HeatzyException as error:
_LOGGER.error("Error to lock pilot : %s", error)


class WindowSwitchEntity(CoordinatorEntity[HeatzyDataUpdateCoordinator], SwitchEntity):
"""Window Switch."""

entity_category = EntityCategory.CONFIG
_attr_has_entity_name = True
_attr_name = "Window detection"

def __init__(
self, coordinator: HeatzyDataUpdateCoordinator, unique_id: str
) -> None:
"""Initialize switch."""
super().__init__(coordinator)
self._attr_unique_id = f"window_{unique_id}"
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, unique_id)})
self._attr = coordinator.data[unique_id].get(CONF_ATTRS, {})

@property
def is_on(self) -> bool:
"""Return true if switch is on."""
return self._attr.get(CONF_WINDOW)
_LOGGER.error("Error to action : %s", error)

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr = self.coordinator.data[self.unique_id].get(CONF_ATTRS, {})
self._attrs = self.coordinator.data.get(self.unique_id, {}).get(CONF_ATTRS, {})
self.async_write_ha_state()

async def async_turn_on(self) -> None:
"""Turn the entity on."""
try:
await self.coordinator.api.websocket.async_control_device(
self.unique_id, {CONF_ATTRS: {CONF_WINDOW: 1}}
)
except HeatzyException as error:
_LOGGER.error("Error to enable window detection : %s", error)

async def async_turn_off(self) -> None:
"""Turn the entity off."""
try:
await self.coordinator.api.websocket.async_control_device(
self.unique_id, {CONF_ATTRS: {CONF_WINDOW: 0}}
)
except HeatzyException as error:
_LOGGER.error("Error to disable window detection : %s", error)

0 comments on commit f36faa7

Please sign in to comment.