Skip to content

Commit

Permalink
Make flexit bacnet switch more generic and prepare for more switches (#…
Browse files Browse the repository at this point in the history
…109154)

Make switch more generic and prepare for more switches
  • Loading branch information
lellky committed Jan 31, 2024
1 parent 4bad88b commit a61b181
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions homeassistant/components/flexit_bacnet/switch.py
@@ -1,6 +1,6 @@
"""The Flexit Nordic (BACnet) integration."""
import asyncio.exceptions
from collections.abc import Callable
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any

Expand All @@ -27,6 +27,8 @@ class FlexitSwitchEntityDescription(SwitchEntityDescription):
"""Describes a Flexit switch entity."""

is_on_fn: Callable[[FlexitBACnet], bool]
turn_on_fn: Callable[[FlexitBACnet], Awaitable[None]]
turn_off_fn: Callable[[FlexitBACnet], Awaitable[None]]


SWITCHES: tuple[FlexitSwitchEntityDescription, ...] = (
Expand All @@ -35,6 +37,8 @@ class FlexitSwitchEntityDescription(SwitchEntityDescription):
translation_key="electric_heater",
icon="mdi:radiator",
is_on_fn=lambda data: data.electric_heater,
turn_on_fn=lambda data: data.enable_electric_heater(),
turn_off_fn=lambda data: data.disable_electric_heater(),
),
)

Expand Down Expand Up @@ -80,7 +84,7 @@ def is_on(self) -> bool:
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn electric heater on."""
try:
await self.device.enable_electric_heater()
await self.entity_description.turn_on_fn(self.coordinator.data)
except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
raise HomeAssistantError from exc
finally:
Expand All @@ -89,7 +93,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn electric heater off."""
try:
await self.device.disable_electric_heater()
await self.entity_description.turn_off_fn(self.coordinator.data)
except (asyncio.exceptions.TimeoutError, ConnectionError, DecodingError) as exc:
raise HomeAssistantError from exc
finally:
Expand Down

0 comments on commit a61b181

Please sign in to comment.