2019-02-13 20:21:14 +00:00
|
|
|
"""Support for AVM Fritz!Box smarthome switch devices."""
|
2018-04-17 10:40:36 +00:00
|
|
|
import requests
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2020-04-20 13:00:07 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_TEMPERATURE,
|
|
|
|
CONF_DEVICES,
|
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTR_STATE_DEVICE_LOCKED,
|
|
|
|
ATTR_STATE_LOCKED,
|
|
|
|
ATTR_TEMPERATURE_UNIT,
|
|
|
|
ATTR_TOTAL_CONSUMPTION,
|
|
|
|
ATTR_TOTAL_CONSUMPTION_UNIT,
|
|
|
|
CONF_CONNECTIONS,
|
|
|
|
DOMAIN as FRITZBOX_DOMAIN,
|
|
|
|
LOGGER,
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Fritzbox smarthome switch from config_entry."""
|
|
|
|
entities = []
|
|
|
|
devices = hass.data[FRITZBOX_DOMAIN][CONF_DEVICES]
|
|
|
|
fritz = hass.data[FRITZBOX_DOMAIN][CONF_CONNECTIONS][config_entry.entry_id]
|
2018-04-17 10:40:36 +00:00
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
for device in await hass.async_add_executor_job(fritz.get_devices):
|
|
|
|
if device.has_switch and device.ain not in devices:
|
|
|
|
entities.append(FritzboxSwitch(device, fritz))
|
|
|
|
devices.add(device.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
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class FritzboxSwitch(SwitchEntity):
|
2018-04-17 10:40:36 +00:00
|
|
|
"""The switch class for Fritzbox switches."""
|
|
|
|
|
|
|
|
def __init__(self, device, fritz):
|
|
|
|
"""Initialize the switch."""
|
|
|
|
self._device = device
|
|
|
|
self._fritz = fritz
|
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device specific attributes."""
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"identifiers": {(FRITZBOX_DOMAIN, self._device.ain)},
|
|
|
|
"manufacturer": self._device.manufacturer,
|
|
|
|
"model": self._device.productname,
|
|
|
|
"sw_version": self._device.fw_version,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID of the device."""
|
|
|
|
return self._device.ain
|
|
|
|
|
2018-04-17 10:40:36 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if switch is available."""
|
|
|
|
return self._device.present
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._device.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the switch is on."""
|
|
|
|
return self._device.switch_state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
|
|
|
self._device.set_switch_state_on()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
self._device.set_switch_state_off()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get latest data and states from the device."""
|
|
|
|
try:
|
|
|
|
self._device.update()
|
|
|
|
except requests.exceptions.HTTPError as ex:
|
2020-04-20 13:00:07 +00:00
|
|
|
LOGGER.warning("Fritzhome connection error: %s", ex)
|
2018-04-17 10:40:36 +00:00
|
|
|
self._fritz.login()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
attrs = {}
|
|
|
|
attrs[ATTR_STATE_DEVICE_LOCKED] = self._device.device_lock
|
|
|
|
attrs[ATTR_STATE_LOCKED] = self._device.lock
|
|
|
|
|
|
|
|
if self._device.has_powermeter:
|
2020-02-25 01:54:20 +00:00
|
|
|
attrs[
|
|
|
|
ATTR_TOTAL_CONSUMPTION
|
|
|
|
] = 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
|
2018-04-17 10:40:36 +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(
|
|
|
|
self._device.temperature, TEMP_CELSIUS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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."""
|
|
|
|
return self._device.power / 1000
|