mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Code cleanup fibaro sensor (#73388)
* Code cleanup fibaro sensor * Adjustments based on code review * Changes from code review, use dict instead of tuple * Remove unneeded deafult in dict get * Another variant to create dict
This commit is contained in:
parent
efb4b10629
commit
f285b6099a
@ -2,11 +2,13 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
ENTITY_ID_FORMAT,
|
ENTITY_ID_FORMAT,
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -27,49 +29,73 @@ from homeassistant.util import convert
|
|||||||
from . import FIBARO_DEVICES, FibaroDevice
|
from . import FIBARO_DEVICES, FibaroDevice
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
# List of known sensors which represents a fibaro device
|
||||||
"com.fibaro.temperatureSensor": [
|
MAIN_SENSOR_TYPES: dict[str, SensorEntityDescription] = {
|
||||||
"Temperature",
|
"com.fibaro.temperatureSensor": SensorEntityDescription(
|
||||||
None,
|
key="com.fibaro.temperatureSensor",
|
||||||
None,
|
name="Temperature",
|
||||||
SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
],
|
),
|
||||||
"com.fibaro.smokeSensor": [
|
"com.fibaro.smokeSensor": SensorEntityDescription(
|
||||||
"Smoke",
|
key="com.fibaro.smokeSensor",
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
name="Smoke",
|
||||||
"mdi:fire",
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||||
None,
|
icon="mdi:fire",
|
||||||
None,
|
),
|
||||||
],
|
"CO2": SensorEntityDescription(
|
||||||
"CO2": [
|
key="CO2",
|
||||||
"CO2",
|
name="CO2",
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||||
None,
|
device_class=SensorDeviceClass.CO2,
|
||||||
SensorDeviceClass.CO2,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
SensorStateClass.MEASUREMENT,
|
),
|
||||||
],
|
"com.fibaro.humiditySensor": SensorEntityDescription(
|
||||||
"com.fibaro.humiditySensor": [
|
key="com.fibaro.humiditySensor",
|
||||||
"Humidity",
|
name="Humidity",
|
||||||
PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
None,
|
device_class=SensorDeviceClass.HUMIDITY,
|
||||||
SensorDeviceClass.HUMIDITY,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
SensorStateClass.MEASUREMENT,
|
),
|
||||||
],
|
"com.fibaro.lightSensor": SensorEntityDescription(
|
||||||
"com.fibaro.lightSensor": [
|
key="com.fibaro.lightSensor",
|
||||||
"Light",
|
name="Light",
|
||||||
LIGHT_LUX,
|
native_unit_of_measurement=LIGHT_LUX,
|
||||||
None,
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
||||||
SensorDeviceClass.ILLUMINANCE,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
SensorStateClass.MEASUREMENT,
|
),
|
||||||
],
|
"com.fibaro.energyMeter": SensorEntityDescription(
|
||||||
"com.fibaro.energyMeter": [
|
key="com.fibaro.energyMeter",
|
||||||
"Energy",
|
name="Energy",
|
||||||
ENERGY_KILO_WATT_HOUR,
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
None,
|
device_class=SensorDeviceClass.ENERGY,
|
||||||
SensorDeviceClass.ENERGY,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
SensorStateClass.TOTAL_INCREASING,
|
),
|
||||||
],
|
}
|
||||||
|
|
||||||
|
# List of additional sensors which are created based on a property
|
||||||
|
# The key is the property name
|
||||||
|
ADDITIONAL_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="energy",
|
||||||
|
name="Energy",
|
||||||
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
|
device_class=SensorDeviceClass.ENERGY,
|
||||||
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="power",
|
||||||
|
name="Power",
|
||||||
|
native_unit_of_measurement=POWER_WATT,
|
||||||
|
device_class=SensorDeviceClass.POWER,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
FIBARO_TO_HASS_UNIT: dict[str, str] = {
|
||||||
|
"lux": LIGHT_LUX,
|
||||||
|
"C": TEMP_CELSIUS,
|
||||||
|
"F": TEMP_FAHRENHEIT,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -80,14 +106,18 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Fibaro controller devices."""
|
"""Set up the Fibaro controller devices."""
|
||||||
entities: list[SensorEntity] = []
|
entities: list[SensorEntity] = []
|
||||||
|
|
||||||
for device in hass.data[DOMAIN][entry.entry_id][FIBARO_DEVICES][Platform.SENSOR]:
|
for device in hass.data[DOMAIN][entry.entry_id][FIBARO_DEVICES][Platform.SENSOR]:
|
||||||
entities.append(FibaroSensor(device))
|
entity_description = MAIN_SENSOR_TYPES.get(device.type)
|
||||||
|
|
||||||
|
# main sensors are created even if the entity type is not known
|
||||||
|
entities.append(FibaroSensor(device, entity_description))
|
||||||
|
|
||||||
for platform in (Platform.COVER, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH):
|
for platform in (Platform.COVER, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH):
|
||||||
for device in hass.data[DOMAIN][entry.entry_id][FIBARO_DEVICES][platform]:
|
for device in hass.data[DOMAIN][entry.entry_id][FIBARO_DEVICES][platform]:
|
||||||
if "energy" in device.interfaces:
|
for entity_description in ADDITIONAL_SENSOR_TYPES:
|
||||||
entities.append(FibaroEnergySensor(device))
|
if entity_description.key in device.properties:
|
||||||
if "power" in device.interfaces:
|
entities.append(FibaroAdditionalSensor(device, entity_description))
|
||||||
entities.append(FibaroPowerSensor(device))
|
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
@ -95,97 +125,51 @@ async def async_setup_entry(
|
|||||||
class FibaroSensor(FibaroDevice, SensorEntity):
|
class FibaroSensor(FibaroDevice, SensorEntity):
|
||||||
"""Representation of a Fibaro Sensor."""
|
"""Representation of a Fibaro Sensor."""
|
||||||
|
|
||||||
def __init__(self, fibaro_device):
|
def __init__(
|
||||||
|
self, fibaro_device: Any, entity_description: SensorEntityDescription | None
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.current_value = None
|
|
||||||
self.last_changed_time = None
|
|
||||||
super().__init__(fibaro_device)
|
super().__init__(fibaro_device)
|
||||||
|
if entity_description is not None:
|
||||||
|
self.entity_description = entity_description
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
|
self.entity_id = ENTITY_ID_FORMAT.format(self.ha_id)
|
||||||
if fibaro_device.type in SENSOR_TYPES:
|
|
||||||
self._unit = SENSOR_TYPES[fibaro_device.type][1]
|
# Map unit if it was not defined in the entity description
|
||||||
self._icon = SENSOR_TYPES[fibaro_device.type][2]
|
# or there is no entity description at all
|
||||||
self._device_class = SENSOR_TYPES[fibaro_device.type][3]
|
|
||||||
self._attr_state_class = SENSOR_TYPES[fibaro_device.type][4]
|
|
||||||
else:
|
|
||||||
self._unit = None
|
|
||||||
self._icon = None
|
|
||||||
self._device_class = None
|
|
||||||
with suppress(KeyError, ValueError):
|
with suppress(KeyError, ValueError):
|
||||||
if not self._unit:
|
if not self.native_unit_of_measurement:
|
||||||
if self.fibaro_device.properties.unit == "lux":
|
self._attr_native_unit_of_measurement = FIBARO_TO_HASS_UNIT.get(
|
||||||
self._unit = LIGHT_LUX
|
fibaro_device.properties.unit, fibaro_device.properties.unit
|
||||||
elif self.fibaro_device.properties.unit == "C":
|
)
|
||||||
self._unit = TEMP_CELSIUS
|
|
||||||
elif self.fibaro_device.properties.unit == "F":
|
|
||||||
self._unit = TEMP_FAHRENHEIT
|
|
||||||
else:
|
|
||||||
self._unit = self.fibaro_device.properties.unit
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self.current_value
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity, if any."""
|
|
||||||
return self._unit
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Icon to use in the frontend, if any."""
|
|
||||||
return self._icon
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the state."""
|
"""Update the state."""
|
||||||
with suppress(KeyError, ValueError):
|
with suppress(KeyError, ValueError):
|
||||||
self.current_value = float(self.fibaro_device.properties.value)
|
self._attr_native_value = float(self.fibaro_device.properties.value)
|
||||||
|
|
||||||
|
|
||||||
class FibaroEnergySensor(FibaroDevice, SensorEntity):
|
class FibaroAdditionalSensor(FibaroDevice, SensorEntity):
|
||||||
"""Representation of a Fibaro Energy Sensor."""
|
"""Representation of a Fibaro Additional Sensor."""
|
||||||
|
|
||||||
_attr_device_class = SensorDeviceClass.ENERGY
|
def __init__(
|
||||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
self, fibaro_device: Any, entity_description: SensorEntityDescription
|
||||||
_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
) -> None:
|
||||||
|
|
||||||
def __init__(self, fibaro_device):
|
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(fibaro_device)
|
super().__init__(fibaro_device)
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(f"{self.ha_id}_energy")
|
self.entity_description = entity_description
|
||||||
self._attr_name = f"{fibaro_device.friendly_name} Energy"
|
|
||||||
self._attr_unique_id = f"{fibaro_device.unique_id_str}_energy"
|
|
||||||
|
|
||||||
def update(self):
|
# To differentiate additional sensors from main sensors they need
|
||||||
|
# to get different names and ids
|
||||||
|
self.entity_id = ENTITY_ID_FORMAT.format(
|
||||||
|
f"{self.ha_id}_{entity_description.key}"
|
||||||
|
)
|
||||||
|
self._attr_name = f"{fibaro_device.friendly_name} {entity_description.name}"
|
||||||
|
self._attr_unique_id = f"{fibaro_device.unique_id_str}_{entity_description.key}"
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
"""Update the state."""
|
"""Update the state."""
|
||||||
with suppress(KeyError, ValueError):
|
with suppress(KeyError, ValueError):
|
||||||
self._attr_native_value = convert(
|
self._attr_native_value = convert(
|
||||||
self.fibaro_device.properties.energy, float
|
self.fibaro_device.properties[self.entity_description.key],
|
||||||
)
|
float,
|
||||||
|
|
||||||
|
|
||||||
class FibaroPowerSensor(FibaroDevice, SensorEntity):
|
|
||||||
"""Representation of a Fibaro Power Sensor."""
|
|
||||||
|
|
||||||
_attr_device_class = SensorDeviceClass.POWER
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
||||||
_attr_native_unit_of_measurement = POWER_WATT
|
|
||||||
|
|
||||||
def __init__(self, fibaro_device):
|
|
||||||
"""Initialize the sensor."""
|
|
||||||
super().__init__(fibaro_device)
|
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(f"{self.ha_id}_power")
|
|
||||||
self._attr_name = f"{fibaro_device.friendly_name} Power"
|
|
||||||
self._attr_unique_id = f"{fibaro_device.unique_id_str}_power"
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
"""Update the state."""
|
|
||||||
with suppress(KeyError, ValueError):
|
|
||||||
self._attr_native_value = convert(
|
|
||||||
self.fibaro_device.properties.power, float
|
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user