2021-05-03 10:52:22 +00:00
|
|
|
"""Support for AVM FRITZ!SmartHome temperature sensor only devices."""
|
2021-05-15 05:54:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
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,
|
|
|
|
)
|
2021-05-15 05:54:11 +00:00
|
|
|
from .model import SensorExtraAttributes
|
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:
|
2021-05-03 10:52:22 +00:00
|
|
|
"""Set up the FRITZ!SmartHome sensor from ConfigEntry."""
|
2021-05-15 05:54:11 +00:00
|
|
|
entities: list[FritzBoxEntity] = []
|
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):
|
2021-05-03 10:52:22 +00:00
|
|
|
"""The entity class for FRITZ!SmartHome sensors."""
|
2021-04-18 22:30:58 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def state(self) -> int | None:
|
2021-04-18 22:30:58 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-05-15 05:54:11 +00:00
|
|
|
return self.device.battery_level # type: ignore [no-any-return]
|
2021-04-18 22:30:58 +00:00
|
|
|
|
|
|
|
|
2021-04-25 00:40:12 +00:00
|
|
|
class FritzBoxTempSensor(FritzBoxEntity, SensorEntity):
|
2021-05-03 10:52:22 +00:00
|
|
|
"""The entity class for FRITZ!SmartHome temperature sensors."""
|
2019-01-30 17:44:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def state(self) -> float | None:
|
2019-01-30 17:44:36 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-05-15 05:54:11 +00:00
|
|
|
return self.device.temperature # type: ignore [no-any-return]
|
2019-01-30 17:44:36 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-15 05:54:11 +00:00
|
|
|
def extra_state_attributes(self) -> SensorExtraAttributes:
|
2019-01-30 17:44:36 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2021-05-15 05:54:11 +00:00
|
|
|
attrs: SensorExtraAttributes = {
|
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
|