1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-29 19:57:40 +01:00
Files
Jamin 6f3dfab487 Voip runtime data (#170765)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2026-05-18 19:40:30 +02:00

61 lines
1.9 KiB
Python

"""VoIP switch entities."""
from typing import Any
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.const import STATE_ON, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import restore_state
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import VoipConfigEntry
from .devices import VoIPDevice
from .entity import VoIPEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: VoipConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up VoIP switch entities."""
domain_data = config_entry.runtime_data.domain_data
@callback
def async_add_device(device: VoIPDevice) -> None:
"""Add device."""
async_add_entities([VoIPCallAllowedSwitch(device)])
domain_data.devices.async_add_new_device_listener(async_add_device)
async_add_entities(
[VoIPCallAllowedSwitch(device) for device in domain_data.devices]
)
class VoIPCallAllowedSwitch(VoIPEntity, restore_state.RestoreEntity, SwitchEntity):
"""Entity to represent voip is allowed."""
entity_description = SwitchEntityDescription(
key="allow_call",
translation_key="allow_call",
entity_category=EntityCategory.CONFIG,
)
async def async_added_to_hass(self) -> None:
"""Call when entity about to be added to hass."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
self._attr_is_on = state is not None and state.state == STATE_ON
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on."""
self._attr_is_on = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off."""
self._attr_is_on = False
self.async_write_ha_state()