Move temperature conversions to sensor base class (5/8) (#54475)

This commit is contained in:
Erik Montnemery 2021-08-12 13:26:17 +02:00 committed by GitHub
parent e55868b17f
commit 103e21c278
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
65 changed files with 289 additions and 288 deletions

View file

@ -83,7 +83,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Temperature",
netatmo_name="Temperature",
entity_registry_enabled_default=True,
unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -98,7 +98,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
key="co2",
name="CO2",
netatmo_name="CO2",
unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
entity_registry_enabled_default=True,
device_class=DEVICE_CLASS_CO2,
state_class=STATE_CLASS_MEASUREMENT,
@ -108,7 +108,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Pressure",
netatmo_name="Pressure",
entity_registry_enabled_default=True,
unit_of_measurement=PRESSURE_MBAR,
native_unit_of_measurement=PRESSURE_MBAR,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -124,7 +124,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Noise",
netatmo_name="Noise",
entity_registry_enabled_default=True,
unit_of_measurement=SOUND_PRESSURE_DB,
native_unit_of_measurement=SOUND_PRESSURE_DB,
icon="mdi:volume-high",
state_class=STATE_CLASS_MEASUREMENT,
),
@ -133,7 +133,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Humidity",
netatmo_name="Humidity",
entity_registry_enabled_default=True,
unit_of_measurement=PERCENTAGE,
native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -142,7 +142,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Rain",
netatmo_name="Rain",
entity_registry_enabled_default=True,
unit_of_measurement=LENGTH_MILLIMETERS,
native_unit_of_measurement=LENGTH_MILLIMETERS,
icon="mdi:weather-rainy",
),
NetatmoSensorEntityDescription(
@ -150,7 +150,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Rain last hour",
netatmo_name="sum_rain_1",
entity_registry_enabled_default=False,
unit_of_measurement=LENGTH_MILLIMETERS,
native_unit_of_measurement=LENGTH_MILLIMETERS,
icon="mdi:weather-rainy",
),
NetatmoSensorEntityDescription(
@ -158,7 +158,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Rain today",
netatmo_name="sum_rain_24",
entity_registry_enabled_default=True,
unit_of_measurement=LENGTH_MILLIMETERS,
native_unit_of_measurement=LENGTH_MILLIMETERS,
icon="mdi:weather-rainy",
),
NetatmoSensorEntityDescription(
@ -166,7 +166,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Battery Percent",
netatmo_name="battery_percent",
entity_registry_enabled_default=True,
unit_of_measurement=PERCENTAGE,
native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -182,7 +182,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Angle",
netatmo_name="WindAngle",
entity_registry_enabled_default=False,
unit_of_measurement=DEGREE,
native_unit_of_measurement=DEGREE,
icon="mdi:compass-outline",
state_class=STATE_CLASS_MEASUREMENT,
),
@ -191,7 +191,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Wind Strength",
netatmo_name="WindStrength",
entity_registry_enabled_default=True,
unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
icon="mdi:weather-windy",
state_class=STATE_CLASS_MEASUREMENT,
),
@ -207,7 +207,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Gust Angle",
netatmo_name="GustAngle",
entity_registry_enabled_default=False,
unit_of_measurement=DEGREE,
native_unit_of_measurement=DEGREE,
icon="mdi:compass-outline",
state_class=STATE_CLASS_MEASUREMENT,
),
@ -216,7 +216,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Gust Strength",
netatmo_name="GustStrength",
entity_registry_enabled_default=False,
unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
icon="mdi:weather-windy",
state_class=STATE_CLASS_MEASUREMENT,
),
@ -239,7 +239,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Radio Level",
netatmo_name="rf_status",
entity_registry_enabled_default=False,
unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -255,7 +255,7 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
name="Wifi Level",
netatmo_name="wifi_status",
entity_registry_enabled_default=False,
unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
),
@ -518,25 +518,25 @@ class NetatmoSensor(NetatmoBase, SensorEntity):
self._device_name,
self._id,
)
self._attr_state = None
self._attr_native_value = None
return
try:
state = data[self.entity_description.netatmo_name]
if self.entity_description.key in {"temperature", "pressure", "sum_rain_1"}:
self._attr_state = round(state, 1)
self._attr_native_value = round(state, 1)
elif self.entity_description.key in {"windangle_value", "gustangle_value"}:
self._attr_state = fix_angle(state)
self._attr_native_value = fix_angle(state)
elif self.entity_description.key in {"windangle", "gustangle"}:
self._attr_state = process_angle(fix_angle(state))
self._attr_native_value = process_angle(fix_angle(state))
elif self.entity_description.key == "rf_status":
self._attr_state = process_rf(state)
self._attr_native_value = process_rf(state)
elif self.entity_description.key == "wifi_status":
self._attr_state = process_wifi(state)
self._attr_native_value = process_wifi(state)
elif self.entity_description.key == "health_idx":
self._attr_state = process_health(state)
self._attr_native_value = process_health(state)
else:
self._attr_state = state
self._attr_native_value = state
except KeyError:
if self.state:
_LOGGER.debug(
@ -544,7 +544,7 @@ class NetatmoSensor(NetatmoBase, SensorEntity):
self.entity_description.key,
self._device_name,
)
self._attr_state = None
self._attr_native_value = None
return
self.async_write_ha_state()
@ -758,14 +758,14 @@ class NetatmoPublicSensor(NetatmoBase, SensorEntity):
self.entity_description.key,
self._area_name,
)
self._attr_state = None
self._attr_native_value = None
return
if values := [x for x in data.values() if x is not None]:
if self._mode == "avg":
self._attr_state = round(sum(values) / len(values), 1)
self._attr_native_value = round(sum(values) / len(values), 1)
elif self._mode == "max":
self._attr_state = max(values)
self._attr_native_value = max(values)
self._attr_available = self.state is not None
self.async_write_ha_state()