2019-02-13 20:21:14 +00:00
|
|
|
"""Support for AVM Fritz!Box smarthome temperature sensor only devices."""
|
2021-03-22 18:45:17 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-18 22:30:58 +00:00
|
|
|
from homeassistant.const import (
|
2021-04-25 00:40:12 +00:00
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
2021-04-18 22:30:58 +00:00
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
PERCENTAGE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2021-04-25 00:40:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +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,
|
2021-04-25 00:40:12 +00:00
|
|
|
CONF_COORDINATOR,
|
2020-04-20 13:00:07 +00:00
|
|
|
DOMAIN as FRITZBOX_DOMAIN,
|
|
|
|
)
|
2019-01-30 17:44:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-30 18:38:59 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-04-25 00:40:12 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Fritzbox smarthome sensor 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]
|
2019-01-30 17:44:36 +00:00
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
for ain, device in coordinator.data.items():
|
2020-04-20 13:00:07 +00:00
|
|
|
if (
|
|
|
|
device.has_temperature_sensor
|
|
|
|
and not device.has_switch
|
|
|
|
and not device.has_thermostat
|
|
|
|
):
|
2021-04-25 00:40:12 +00:00
|
|
|
entities.append(
|
|
|
|
FritzBoxTempSensor(
|
|
|
|
{
|
|
|
|
ATTR_NAME: f"{device.name}",
|
|
|
|
ATTR_ENTITY_ID: f"{device.ain}",
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS,
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
},
|
|
|
|
coordinator,
|
|
|
|
ain,
|
|
|
|
)
|
|
|
|
)
|
2019-01-30 17:44:36 +00:00
|
|
|
|
2021-04-18 22:30:58 +00:00
|
|
|
if device.battery_level is not None:
|
2021-04-25 00:40:12 +00:00
|
|
|
entities.append(
|
|
|
|
FritzBoxBatterySensor(
|
|
|
|
{
|
|
|
|
ATTR_NAME: f"{device.name} Battery",
|
|
|
|
ATTR_ENTITY_ID: f"{device.ain}_battery",
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
|
|
|
|
ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY,
|
|
|
|
},
|
|
|
|
coordinator,
|
|
|
|
ain,
|
|
|
|
)
|
|
|
|
)
|
2021-04-18 22:30:58 +00:00
|
|
|
|
2020-04-20 13:00:07 +00:00
|
|
|
async_add_entities(entities)
|
2019-01-30 17:44:36 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
class FritzBoxBatterySensor(FritzBoxEntity, SensorEntity):
|
|
|
|
"""The entity class for Fritzbox sensors."""
|
2021-04-18 22:30:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2021-04-25 00:40:12 +00:00
|
|
|
return self.device.battery_level
|
2021-04-18 22:30:58 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
class FritzBoxTempSensor(FritzBoxEntity, SensorEntity):
|
2019-01-30 17:44:36 +00:00
|
|
|
"""The entity class for Fritzbox temperature sensors."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2021-04-25 00:40:12 +00:00
|
|
|
return self.device.temperature
|
2019-01-30 17:44:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-01-30 17:44:36 +00:00
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
attrs = {
|
2021-04-25 00:40:12 +00:00
|
|
|
ATTR_STATE_DEVICE_LOCKED: self.device.device_lock,
|
|
|
|
ATTR_STATE_LOCKED: self.device.lock,
|
2019-01-30 17:44:36 +00:00
|
|
|
}
|
|
|
|
return attrs
|