Use new enums in fritzbox (#61447)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-10 14:58:23 +01:00 committed by GitHub
parent 0d9d6d5727
commit 44b7c0e65c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 26 deletions

View File

@ -8,7 +8,7 @@ from typing import Final
from pyfritzhome.fritzhomedevice import FritzhomeDevice from pyfritzhome.fritzhomedevice import FritzhomeDevice
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_WINDOW, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
@ -40,7 +40,7 @@ BINARY_SENSOR_TYPES: Final[tuple[FritzBinarySensorEntityDescription, ...]] = (
FritzBinarySensorEntityDescription( FritzBinarySensorEntityDescription(
key="alarm", key="alarm",
name="Alarm", name="Alarm",
device_class=DEVICE_CLASS_WINDOW, device_class=BinarySensorDeviceClass.WINDOW,
suitable=lambda device: device.has_alarm, # type: ignore[no-any-return] suitable=lambda device: device.has_alarm, # type: ignore[no-any-return]
is_on=lambda device: device.alert_state, # type: ignore[no-any-return] is_on=lambda device: device.alert_state, # type: ignore[no-any-return]
), ),

View File

@ -10,26 +10,20 @@ from pyfritzhome.fritzhomedevice import FritzhomeDevice
from homeassistant.components.climate.const import PRESET_COMFORT, PRESET_ECO from homeassistant.components.climate.const import PRESET_COMFORT, PRESET_ECO
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT, SensorDeviceClass,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
ENTITY_CATEGORY_DIAGNOSTIC,
PERCENTAGE, PERCENTAGE,
POWER_WATT, POWER_WATT,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import utc_from_timestamp from homeassistant.util.dt import utc_from_timestamp
@ -58,9 +52,9 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="temperature", key="temperature",
name="Temperature", name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
suitable=lambda device: ( suitable=lambda device: (
device.has_temperature_sensor and not device.has_thermostat device.has_temperature_sensor and not device.has_thermostat
), ),
@ -70,8 +64,8 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="humidity", key="humidity",
name="Humidity", name="Humidity",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
suitable=lambda device: device.rel_humidity is not None, suitable=lambda device: device.rel_humidity is not None,
native_value=lambda device: device.rel_humidity, # type: ignore[no-any-return] native_value=lambda device: device.rel_humidity, # type: ignore[no-any-return]
), ),
@ -79,8 +73,8 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="battery", key="battery",
name="Battery", name="Battery",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_BATTERY, device_class=SensorDeviceClass.BATTERY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
suitable=lambda device: device.battery_level is not None, suitable=lambda device: device.battery_level is not None,
native_value=lambda device: device.battery_level, # type: ignore[no-any-return] native_value=lambda device: device.battery_level, # type: ignore[no-any-return]
), ),
@ -88,8 +82,8 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="power_consumption", key="power_consumption",
name="Power Consumption", name="Power Consumption",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=SensorDeviceClass.POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
suitable=lambda device: device.has_powermeter, # type: ignore[no-any-return] suitable=lambda device: device.has_powermeter, # type: ignore[no-any-return]
native_value=lambda device: device.power / 1000 if device.power else 0.0, native_value=lambda device: device.power / 1000 if device.power else 0.0,
), ),
@ -97,8 +91,8 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="total_energy", key="total_energy",
name="Total Energy", name="Total Energy",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=SensorDeviceClass.ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
suitable=lambda device: device.has_powermeter, # type: ignore[no-any-return] suitable=lambda device: device.has_powermeter, # type: ignore[no-any-return]
native_value=lambda device: device.energy / 1000 if device.energy else 0.0, native_value=lambda device: device.energy / 1000 if device.energy else 0.0,
), ),
@ -107,7 +101,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="comfort_temperature", key="comfort_temperature",
name="Comfort Temperature", name="Comfort Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
suitable=lambda device: device.has_thermostat suitable=lambda device: device.has_thermostat
and device.comfort_temperature is not None, and device.comfort_temperature is not None,
native_value=lambda device: device.comfort_temperature, # type: ignore[no-any-return] native_value=lambda device: device.comfort_temperature, # type: ignore[no-any-return]
@ -116,7 +110,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="eco_temperature", key="eco_temperature",
name="Eco Temperature", name="Eco Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
suitable=lambda device: device.has_thermostat suitable=lambda device: device.has_thermostat
and device.eco_temperature is not None, and device.eco_temperature is not None,
native_value=lambda device: device.eco_temperature, # type: ignore[no-any-return] native_value=lambda device: device.eco_temperature, # type: ignore[no-any-return]
@ -125,7 +119,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
key="nextchange_temperature", key="nextchange_temperature",
name="Next Scheduled Temperature", name="Next Scheduled Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
suitable=lambda device: device.has_thermostat suitable=lambda device: device.has_thermostat
and device.nextchange_temperature is not None, and device.nextchange_temperature is not None,
native_value=lambda device: device.nextchange_temperature, # type: ignore[no-any-return] native_value=lambda device: device.nextchange_temperature, # type: ignore[no-any-return]
@ -133,7 +127,7 @@ SENSOR_TYPES: Final[tuple[FritzSensorEntityDescription, ...]] = (
FritzSensorEntityDescription( FritzSensorEntityDescription(
key="nextchange_time", key="nextchange_time",
name="Next Scheduled Change Time", name="Next Scheduled Change Time",
device_class=DEVICE_CLASS_TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
suitable=lambda device: device.has_thermostat suitable=lambda device: device.has_thermostat
and device.nextchange_endperiod is not None, and device.nextchange_endperiod is not None,
native_value=lambda device: utc_from_timestamp(device.nextchange_endperiod), native_value=lambda device: utc_from_timestamp(device.nextchange_endperiod),