1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-19 06:50:15 +01:00
Files
core/homeassistant/components/abode/switch.py
T
2026-04-30 21:14:48 +02:00

90 lines
2.6 KiB
Python

"""Support for Abode Security System switches."""
from typing import Any, cast
from jaraco.abode.devices.switch import Switch
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import AbodeConfigEntry
from .entity import AbodeAutomation, AbodeDevice
DEVICE_TYPES = ["switch", "valve"]
async def async_setup_entry(
hass: HomeAssistant,
entry: AbodeConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Abode switch devices."""
data = entry.runtime_data
entities: list[SwitchEntity] = [
AbodeSwitch(data, device)
for device_type in DEVICE_TYPES
for device in data.abode.get_devices(generic_type=device_type)
]
entities.extend(
AbodeAutomationSwitch(data, automation)
for automation in data.abode.get_automations()
)
async_add_entities(entities)
class AbodeSwitch(AbodeDevice, SwitchEntity):
"""Representation of an Abode switch."""
_device: Switch
_attr_name = None
def turn_on(self, **kwargs: Any) -> None:
"""Turn on the device."""
self._device.switch_on()
def turn_off(self, **kwargs: Any) -> None:
"""Turn off the device."""
self._device.switch_off()
@property
def is_on(self) -> bool:
"""Return true if device is on."""
return cast(bool, self._device.is_on)
class AbodeAutomationSwitch(AbodeAutomation, SwitchEntity):
"""A switch implementation for Abode automations."""
_attr_translation_key = "automation"
async def async_added_to_hass(self) -> None:
"""Set up trigger automation service."""
await super().async_added_to_hass()
signal = f"abode_trigger_automation_{self.entity_id}"
self.async_on_remove(async_dispatcher_connect(self.hass, signal, self.trigger))
def turn_on(self, **kwargs: Any) -> None:
"""Enable the automation."""
if self._automation.enable(True):
self.schedule_update_ha_state()
def turn_off(self, **kwargs: Any) -> None:
"""Disable the automation."""
if self._automation.enable(False):
self.schedule_update_ha_state()
def trigger(self) -> None:
"""Trigger the automation."""
self._automation.trigger()
@property
def is_on(self) -> bool:
"""Return True if the automation is enabled."""
return bool(self._automation.enabled)