mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Split out shared screenlogic switch code (#106344)
This commit is contained in:
parent
9066555feb
commit
f45f0b4327
@ -22,7 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from .const import DOMAIN as SL_DOMAIN
|
from .const import DOMAIN as SL_DOMAIN
|
||||||
from .coordinator import ScreenlogicDataUpdateCoordinator
|
from .coordinator import ScreenlogicDataUpdateCoordinator
|
||||||
from .entity import (
|
from .entity import (
|
||||||
ScreenlogicEntity,
|
ScreenLogicEntity,
|
||||||
ScreenLogicEntityDescription,
|
ScreenLogicEntityDescription,
|
||||||
ScreenLogicPushEntity,
|
ScreenLogicPushEntity,
|
||||||
ScreenLogicPushEntityDescription,
|
ScreenLogicPushEntityDescription,
|
||||||
@ -232,7 +232,7 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicBinarySensor(ScreenlogicEntity, BinarySensorEntity):
|
class ScreenLogicBinarySensor(ScreenLogicEntity, BinarySensorEntity):
|
||||||
"""Representation of a ScreenLogic binary sensor entity."""
|
"""Representation of a ScreenLogic binary sensor entity."""
|
||||||
|
|
||||||
entity_description: ScreenLogicBinarySensorDescription
|
entity_description: ScreenLogicBinarySensorDescription
|
||||||
|
@ -44,7 +44,7 @@ class ScreenLogicEntityDescription(
|
|||||||
enabled_lambda: Callable[..., bool] | None = None
|
enabled_lambda: Callable[..., bool] | None = None
|
||||||
|
|
||||||
|
|
||||||
class ScreenlogicEntity(CoordinatorEntity[ScreenlogicDataUpdateCoordinator]):
|
class ScreenLogicEntity(CoordinatorEntity[ScreenlogicDataUpdateCoordinator]):
|
||||||
"""Base class for all ScreenLogic entities."""
|
"""Base class for all ScreenLogic entities."""
|
||||||
|
|
||||||
entity_description: ScreenLogicEntityDescription
|
entity_description: ScreenLogicEntityDescription
|
||||||
@ -118,7 +118,7 @@ class ScreenLogicPushEntityDescription(
|
|||||||
"""Base class for a ScreenLogic push entity description."""
|
"""Base class for a ScreenLogic push entity description."""
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicPushEntity(ScreenlogicEntity):
|
class ScreenLogicPushEntity(ScreenLogicEntity):
|
||||||
"""Base class for all ScreenLogic push entities."""
|
"""Base class for all ScreenLogic push entities."""
|
||||||
|
|
||||||
entity_description: ScreenLogicPushEntityDescription
|
entity_description: ScreenLogicPushEntityDescription
|
||||||
@ -157,8 +157,8 @@ class ScreenLogicPushEntity(ScreenlogicEntity):
|
|||||||
self._async_data_updated()
|
self._async_data_updated()
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicCircuitEntity(ScreenLogicPushEntity):
|
class ScreenLogicSwitchingEntity(ScreenLogicEntity):
|
||||||
"""Base class for all ScreenLogic switch and light entities."""
|
"""Base class for all switchable entities."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
@ -167,13 +167,20 @@ class ScreenLogicCircuitEntity(ScreenLogicPushEntity):
|
|||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Send the ON command."""
|
"""Send the ON command."""
|
||||||
await self._async_set_circuit(ON_OFF.ON)
|
await self._async_set_state(ON_OFF.ON)
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Send the OFF command."""
|
"""Send the OFF command."""
|
||||||
await self._async_set_circuit(ON_OFF.OFF)
|
await self._async_set_state(ON_OFF.OFF)
|
||||||
|
|
||||||
async def _async_set_circuit(self, state: ON_OFF) -> None:
|
async def _async_set_state(self, state: ON_OFF) -> None:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
class ScreenLogicCircuitEntity(ScreenLogicSwitchingEntity, ScreenLogicPushEntity):
|
||||||
|
"""Base class for all ScreenLogic circuit switch and light entities."""
|
||||||
|
|
||||||
|
async def _async_set_state(self, state: ON_OFF) -> None:
|
||||||
try:
|
try:
|
||||||
await self.gateway.async_set_circuit(self._data_key, state.value)
|
await self.gateway.async_set_circuit(self._data_key, state.value)
|
||||||
except (ScreenLogicCommunicationError, ScreenLogicError) as sle:
|
except (ScreenLogicCommunicationError, ScreenLogicError) as sle:
|
||||||
|
@ -21,7 +21,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
|
|
||||||
from .const import DOMAIN as SL_DOMAIN
|
from .const import DOMAIN as SL_DOMAIN
|
||||||
from .coordinator import ScreenlogicDataUpdateCoordinator
|
from .coordinator import ScreenlogicDataUpdateCoordinator
|
||||||
from .entity import ScreenlogicEntity, ScreenLogicEntityDescription
|
from .entity import ScreenLogicEntity, ScreenLogicEntityDescription
|
||||||
from .util import cleanup_excluded_entity, get_ha_unit
|
from .util import cleanup_excluded_entity, get_ha_unit
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -87,7 +87,7 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicNumber(ScreenlogicEntity, NumberEntity):
|
class ScreenLogicNumber(ScreenLogicEntity, NumberEntity):
|
||||||
"""Class to represent a ScreenLogic Number entity."""
|
"""Class to represent a ScreenLogic Number entity."""
|
||||||
|
|
||||||
entity_description: ScreenLogicNumberDescription
|
entity_description: ScreenLogicNumberDescription
|
||||||
|
@ -25,7 +25,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from .const import DOMAIN as SL_DOMAIN
|
from .const import DOMAIN as SL_DOMAIN
|
||||||
from .coordinator import ScreenlogicDataUpdateCoordinator
|
from .coordinator import ScreenlogicDataUpdateCoordinator
|
||||||
from .entity import (
|
from .entity import (
|
||||||
ScreenlogicEntity,
|
ScreenLogicEntity,
|
||||||
ScreenLogicEntityDescription,
|
ScreenLogicEntityDescription,
|
||||||
ScreenLogicPushEntity,
|
ScreenLogicPushEntity,
|
||||||
ScreenLogicPushEntityDescription,
|
ScreenLogicPushEntityDescription,
|
||||||
@ -295,7 +295,7 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicSensor(ScreenlogicEntity, SensorEntity):
|
class ScreenLogicSensor(ScreenLogicEntity, SensorEntity):
|
||||||
"""Representation of a ScreenLogic sensor entity."""
|
"""Representation of a ScreenLogic sensor entity."""
|
||||||
|
|
||||||
entity_description: ScreenLogicSensorDescription
|
entity_description: ScreenLogicSensorDescription
|
||||||
|
@ -13,18 +13,29 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
|
|
||||||
from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS
|
from .const import DOMAIN as SL_DOMAIN, LIGHT_CIRCUIT_FUNCTIONS
|
||||||
from .coordinator import ScreenlogicDataUpdateCoordinator
|
from .coordinator import ScreenlogicDataUpdateCoordinator
|
||||||
from .entity import ScreenLogicCircuitEntity, ScreenLogicPushEntityDescription
|
from .entity import (
|
||||||
|
ScreenLogicCircuitEntity,
|
||||||
|
ScreenLogicPushEntityDescription,
|
||||||
|
ScreenLogicSwitchingEntity,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ScreenLogicCircuitSwitchDescription(
|
||||||
|
SwitchEntityDescription, ScreenLogicPushEntityDescription
|
||||||
|
):
|
||||||
|
"""Describes a ScreenLogic switch entity."""
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up entry."""
|
"""Set up entry."""
|
||||||
entities: list[ScreenLogicSwitch] = []
|
entities: list[ScreenLogicSwitchingEntity] = []
|
||||||
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][
|
coordinator: ScreenlogicDataUpdateCoordinator = hass.data[SL_DOMAIN][
|
||||||
config_entry.entry_id
|
config_entry.entry_id
|
||||||
]
|
]
|
||||||
@ -39,9 +50,9 @@ async def async_setup_entry(
|
|||||||
circuit_name = circuit_data[ATTR.NAME]
|
circuit_name = circuit_data[ATTR.NAME]
|
||||||
circuit_interface = INTERFACE(circuit_data[ATTR.INTERFACE])
|
circuit_interface = INTERFACE(circuit_data[ATTR.INTERFACE])
|
||||||
entities.append(
|
entities.append(
|
||||||
ScreenLogicSwitch(
|
ScreenLogicCircuitSwitch(
|
||||||
coordinator,
|
coordinator,
|
||||||
ScreenLogicSwitchDescription(
|
ScreenLogicCircuitSwitchDescription(
|
||||||
subscription_code=CODE.STATUS_CHANGED,
|
subscription_code=CODE.STATUS_CHANGED,
|
||||||
data_root=(DEVICE.CIRCUIT,),
|
data_root=(DEVICE.CIRCUIT,),
|
||||||
key=circuit_index,
|
key=circuit_index,
|
||||||
@ -56,14 +67,7 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
class ScreenLogicCircuitSwitch(ScreenLogicCircuitEntity, SwitchEntity):
|
||||||
class ScreenLogicSwitchDescription(
|
|
||||||
SwitchEntityDescription, ScreenLogicPushEntityDescription
|
|
||||||
):
|
|
||||||
"""Describes a ScreenLogic switch entity."""
|
|
||||||
|
|
||||||
|
|
||||||
class ScreenLogicSwitch(ScreenLogicCircuitEntity, SwitchEntity):
|
|
||||||
"""Class to represent a ScreenLogic Switch."""
|
"""Class to represent a ScreenLogic Switch."""
|
||||||
|
|
||||||
entity_description: ScreenLogicSwitchDescription
|
entity_description: ScreenLogicCircuitSwitchDescription
|
||||||
|
Loading…
x
Reference in New Issue
Block a user