Add switch platform to eq3btsmart (#130363)

This commit is contained in:
Lennard Beers 2024-11-12 11:42:25 +01:00 committed by GitHub
parent 7045b776b6
commit 7758d8ba48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 141 additions and 0 deletions

View file

@ -21,6 +21,7 @@ from .models import Eq3Config, Eq3ConfigEntryData
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.CLIMATE,
Platform.SWITCH,
]
_LOGGER = logging.getLogger(__name__)

View file

@ -21,6 +21,9 @@ DEVICE_MODEL = "CC-RT-BLE-EQ"
ENTITY_KEY_DST = "dst"
ENTITY_KEY_BATTERY = "battery"
ENTITY_KEY_WINDOW = "window"
ENTITY_KEY_LOCK = "lock"
ENTITY_KEY_BOOST = "boost"
ENTITY_KEY_AWAY = "away"
GET_DEVICE_TIMEOUT = 5 # seconds

View file

@ -0,0 +1,32 @@
{
"entity": {
"binary_sensor": {
"dst": {
"default": "mdi:sun-clock",
"state": {
"off": "mdi:sun-clock-outline"
}
}
},
"switch": {
"away": {
"default": "mdi:home-account",
"state": {
"on": "mdi:home-export"
}
},
"lock": {
"default": "mdi:lock",
"state": {
"off": "mdi:lock-off"
}
},
"boost": {
"default": "mdi:fire",
"state": {
"off": "mdi:fire-off"
}
}
}
}
}

View file

@ -24,6 +24,17 @@
"dst": {
"name": "Daylight saving time"
}
},
"switch": {
"lock": {
"name": "Lock"
},
"boost": {
"name": "Boost"
},
"away": {
"name": "Away"
}
}
}
}

View file

@ -0,0 +1,94 @@
"""Platform for eq3 switch entities."""
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from eq3btsmart import Thermostat
from eq3btsmart.models import Status
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import Eq3ConfigEntry
from .const import ENTITY_KEY_AWAY, ENTITY_KEY_BOOST, ENTITY_KEY_LOCK
from .entity import Eq3Entity
@dataclass(frozen=True, kw_only=True)
class Eq3SwitchEntityDescription(SwitchEntityDescription):
"""Entity description for eq3 switch entities."""
toggle_func: Callable[[Thermostat], Callable[[bool], Awaitable[None]]]
value_func: Callable[[Status], bool]
SWITCH_ENTITY_DESCRIPTIONS = [
Eq3SwitchEntityDescription(
key=ENTITY_KEY_LOCK,
translation_key=ENTITY_KEY_LOCK,
toggle_func=lambda thermostat: thermostat.async_set_locked,
value_func=lambda status: status.is_locked,
),
Eq3SwitchEntityDescription(
key=ENTITY_KEY_BOOST,
translation_key=ENTITY_KEY_BOOST,
toggle_func=lambda thermostat: thermostat.async_set_boost,
value_func=lambda status: status.is_boost,
),
Eq3SwitchEntityDescription(
key=ENTITY_KEY_AWAY,
translation_key=ENTITY_KEY_AWAY,
toggle_func=lambda thermostat: thermostat.async_set_away,
value_func=lambda status: status.is_away,
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: Eq3ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the entry."""
async_add_entities(
Eq3SwitchEntity(entry, entity_description)
for entity_description in SWITCH_ENTITY_DESCRIPTIONS
)
class Eq3SwitchEntity(Eq3Entity, SwitchEntity):
"""Base class for eq3 switch entities."""
entity_description: Eq3SwitchEntityDescription
def __init__(
self,
entry: Eq3ConfigEntry,
entity_description: Eq3SwitchEntityDescription,
) -> None:
"""Initialize the entity."""
super().__init__(entry, entity_description.key)
self.entity_description = entity_description
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the switch."""
await self.entity_description.toggle_func(self._thermostat)(True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the switch."""
await self.entity_description.toggle_func(self._thermostat)(False)
@property
def is_on(self) -> bool:
"""Return the state of the switch."""
if TYPE_CHECKING:
assert self._thermostat.status is not None
return self.entity_description.value_func(self._thermostat.status)