diff --git a/custom_components/dbuezas_eq3btsmart/__init__.py b/custom_components/dbuezas_eq3btsmart/__init__.py index b2fc49b..2b8e03d 100644 --- a/custom_components/dbuezas_eq3btsmart/__init__.py +++ b/custom_components/dbuezas_eq3btsmart/__init__.py @@ -21,6 +21,7 @@ PLATFORMS = [ Platform.CLIMATE, Platform.BUTTON, + Platform.LOCK, Platform.SENSOR, Platform.SWITCH, Platform.BINARY_SENSOR, diff --git a/custom_components/dbuezas_eq3btsmart/lock.py b/custom_components/dbuezas_eq3btsmart/lock.py new file mode 100644 index 0000000..70ab9c9 --- /dev/null +++ b/custom_components/dbuezas_eq3btsmart/lock.py @@ -0,0 +1,61 @@ +from .const import DOMAIN +import logging + +from homeassistant.helpers.device_registry import format_mac +from .python_eq3bt.eq3bt.eq3btsmart import Mode, Thermostat +from homeassistant.helpers.entity import DeviceInfo, EntityCategory +from homeassistant.components.lock import LockEntity +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + eq3 = hass.data[DOMAIN][config_entry.entry_id] + + new_devices = [ + LockedSwitch(eq3), + ] + async_add_entities(new_devices) + + +class Base(LockEntity): + def __init__(self, _thermostat: Thermostat): + self._thermostat = _thermostat + self._attr_has_entity_name = True + + @property + def unique_id(self) -> str: + assert self.name + return format_mac(self._thermostat.mac) + "_" + self.name + + @property + def device_info(self) -> DeviceInfo: + return DeviceInfo( + identifiers={(DOMAIN, self._thermostat.mac)}, + ) + + + +class LockedSwitch(Base): + def __init__(self, _thermostat: Thermostat): + super().__init__(_thermostat) + _thermostat.register_update_callback(self.schedule_update_ha_state) + self._attr_name = "Locked" + + async def async_lock(self): + await self._thermostat.async_set_locked(True) + + async def async_unlock(self): + await self._thermostat.async_set_locked(False) + + @property + def is_locked(self): + return self._thermostat.locked + diff --git a/custom_components/dbuezas_eq3btsmart/switch.py b/custom_components/dbuezas_eq3btsmart/switch.py index 8ebb96a..7987d55 100644 --- a/custom_components/dbuezas_eq3btsmart/switch.py +++ b/custom_components/dbuezas_eq3btsmart/switch.py @@ -21,7 +21,6 @@ async def async_setup_entry( debug_mode = config_entry.options.get(CONF_DEBUG_MODE, False) new_devices = [ - LockedSwitch(eq3), AwaySwitch(eq3), BoostSwitch(eq3), ] @@ -48,24 +47,6 @@ def device_info(self) -> DeviceInfo: ) -class LockedSwitch(Base): - def __init__(self, _thermostat: Thermostat): - super().__init__(_thermostat) - _thermostat.register_update_callback(self.schedule_update_ha_state) - self._attr_name = "Locked" - self._attr_icon = "mdi:lock" - - async def async_turn_on(self): - await self._thermostat.async_set_locked(True) - - async def async_turn_off(self): - await self._thermostat.async_set_locked(False) - - @property - def is_on(self): - return self._thermostat.locked - - class AwaySwitch(Base): def __init__(self, _thermostat: Thermostat): super().__init__(_thermostat)