1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/fritzbox/switch.py
Michael e2dac31471
Use EntityDescription - fritzbox (#55104)
* Use sensor entity description

* check if not none instead if callable

* List comprehension in switch and climate

* change state to native_value in description

* merge FritzBoxSensorEntity into FritzBoxEntity

* rename SENSOR_DESCRIPTIONS to SENSOR_TYPES

* use mixins for descriptions

* use comprehension in async_setup_entry()

* improve extra_state_attributes
2021-08-27 17:09:34 +02:00

47 lines
1.6 KiB
Python

"""Support for AVM FRITZ!SmartHome switch devices."""
from __future__ import annotations
from typing import Any
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import FritzBoxEntity
from .const import CONF_COORDINATOR, DOMAIN as FRITZBOX_DOMAIN
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the FRITZ!SmartHome switch from ConfigEntry."""
coordinator = hass.data[FRITZBOX_DOMAIN][entry.entry_id][CONF_COORDINATOR]
async_add_entities(
[
FritzboxSwitch(coordinator, ain)
for ain, device in coordinator.data.items()
if device.has_switch
]
)
class FritzboxSwitch(FritzBoxEntity, SwitchEntity):
"""The switch class for FRITZ!SmartHome switches."""
@property
def is_on(self) -> bool:
"""Return true if the switch is on."""
return self.device.switch_state # type: ignore [no-any-return]
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self.hass.async_add_executor_job(self.device.set_switch_state_on)
await self.coordinator.async_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self.hass.async_add_executor_job(self.device.set_switch_state_off)
await self.coordinator.async_refresh()