2019-02-13 20:21:14 +00:00
|
|
|
"""Support for AVM Fritz!Box smarthome switch devices."""
|
2021-04-25 00:40:12 +00:00
|
|
|
from typing import Callable
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-04-20 13:00:07 +00:00
|
|
|
from homeassistant.const import (
|
2021-04-25 00:40:12 +00:00
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_NAME,
|
2020-04-20 13:00:07 +00:00
|
|
|
ATTR_TEMPERATURE,
|
2021-04-25 00:40:12 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
2020-04-20 13:00:07 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-04-20 13:00:07 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
from . import FritzBoxEntity
|
2020-04-20 13:00:07 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_STATE_DEVICE_LOCKED,
|
|
|
|
ATTR_STATE_LOCKED,
|
|
|
|
ATTR_TEMPERATURE_UNIT,
|
|
|
|
ATTR_TOTAL_CONSUMPTION,
|
|
|
|
ATTR_TOTAL_CONSUMPTION_UNIT,
|
2021-04-25 00:40:12 +00:00
|
|
|
CONF_COORDINATOR,
|
2020-04-20 13:00:07 +00:00
|
|
|
DOMAIN as FRITZBOX_DOMAIN,
|
|
|
|
)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2019-03-13 01:46:41 +00:00
|
|
|
ATTR_TOTAL_CONSUMPTION_UNIT_VALUE = ENERGY_KILO_WATT_HOUR
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: Callable
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Fritzbox smarthome switch from ConfigEntry."""
|
2020-04-20 13:00:07 +00:00
|
|
|
entities = []
|
2021-04-25 00:40:12 +00:00
|
|
|
coordinator = hass.data[FRITZBOX_DOMAIN][entry.entry_id][CONF_COORDINATOR]
|
|
|
|
|
|
|
|
for ain, device in coordinator.data.items():
|
|
|
|
if not device.has_switch:
|
|
|
|
continue
|
|
|
|
|
|
|
|
entities.append(
|
|
|
|
FritzboxSwitch(
|
|
|
|
{
|
|
|
|
ATTR_NAME: f"{device.name}",
|
|
|
|
ATTR_ENTITY_ID: f"{device.ain}",
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT: None,
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
},
|
|
|
|
coordinator,
|
|
|
|
ain,
|
|
|
|
)
|
|
|
|
)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
async_add_entities(entities)
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
class FritzboxSwitch(FritzBoxEntity, SwitchEntity):
|
2018-04-17 10:40:36 +00:00
|
|
|
"""The switch class for Fritzbox switches."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if switch is available."""
|
2021-04-25 00:40:12 +00:00
|
|
|
return self.device.present
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the switch is on."""
|
2021-04-25 00:40:12 +00:00
|
|
|
return self.device.switch_state
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Turn the switch on."""
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.hass.async_add_executor_job(self.device.set_switch_state_on)
|
|
|
|
await self.coordinator.async_refresh()
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Turn the switch off."""
|
2021-04-25 00:40:12 +00:00
|
|
|
await self.hass.async_add_executor_job(self.device.set_switch_state_off)
|
|
|
|
await self.coordinator.async_refresh()
|
2018-04-17 10:40:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-04-17 10:40:36 +00:00
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
attrs = {}
|
2021-04-25 00:40:12 +00:00
|
|
|
attrs[ATTR_STATE_DEVICE_LOCKED] = self.device.device_lock
|
|
|
|
attrs[ATTR_STATE_LOCKED] = self.device.lock
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
if self.device.has_powermeter:
|
2020-02-25 01:54:20 +00:00
|
|
|
attrs[
|
|
|
|
ATTR_TOTAL_CONSUMPTION
|
2021-04-25 00:40:12 +00:00
|
|
|
] = f"{((self.device.energy or 0.0) / 1000):.3f}"
|
2019-07-31 19:25:30 +00:00
|
|
|
attrs[ATTR_TOTAL_CONSUMPTION_UNIT] = ATTR_TOTAL_CONSUMPTION_UNIT_VALUE
|
2021-04-25 00:40:12 +00:00
|
|
|
if self.device.has_temperature_sensor:
|
2019-07-31 19:25:30 +00:00
|
|
|
attrs[ATTR_TEMPERATURE] = str(
|
|
|
|
self.hass.config.units.temperature(
|
2021-04-25 00:40:12 +00:00
|
|
|
self.device.temperature, TEMP_CELSIUS
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
attrs[ATTR_TEMPERATURE_UNIT] = self.hass.config.units.temperature_unit
|
2018-04-17 10:40:36 +00:00
|
|
|
return attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power usage in W."""
|
2021-04-25 00:40:12 +00:00
|
|
|
return self.device.power / 1000
|