From 87e7a8fb5f5a0108acf0349f195f1c43fe58adda Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sun, 15 Aug 2021 08:51:43 +0200 Subject: [PATCH] Move temperature conversions to sensor base class - new integrations (#54623) * Move temperature conversions to sensor base class * Tweaks * Update pvpc_hourly_pricing * Fix flipr sensor * Fix ezviz and youless sensor --- homeassistant/components/acmeda/sensor.py | 2 +- .../components/advantage_air/sensor.py | 2 +- homeassistant/components/canary/sensor.py | 2 +- homeassistant/components/ezviz/sensor.py | 5 ++- homeassistant/components/flipr/sensor.py | 8 ++-- homeassistant/components/fritzbox/sensor.py | 4 +- homeassistant/components/fronius/sensor.py | 6 +-- homeassistant/components/mill/sensor.py | 6 +-- homeassistant/components/powerwall/sensor.py | 4 +- .../components/pvpc_hourly_pricing/sensor.py | 9 ++-- homeassistant/components/renault/sensor.py | 42 +++++++++---------- .../components/smartthings/sensor.py | 4 +- homeassistant/components/spider/sensor.py | 8 ++-- homeassistant/components/tplink/sensor.py | 2 +- homeassistant/components/wemo/sensor.py | 4 +- .../components/xiaomi_miio/sensor.py | 2 +- homeassistant/components/youless/sensor.py | 8 ++-- 17 files changed, 60 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/acmeda/sensor.py b/homeassistant/components/acmeda/sensor.py index 7cded0adb30..43f5e32c74f 100644 --- a/homeassistant/components/acmeda/sensor.py +++ b/homeassistant/components/acmeda/sensor.py @@ -34,7 +34,7 @@ class AcmedaBattery(AcmedaBase, SensorEntity): """Representation of a Acmeda cover device.""" device_class = DEVICE_CLASS_BATTERY - unit_of_measurement = PERCENTAGE + _attr_native_unit_of_measurement = PERCENTAGE @property def name(self): diff --git a/homeassistant/components/advantage_air/sensor.py b/homeassistant/components/advantage_air/sensor.py index 65b7b35740e..5912101fd65 100644 --- a/homeassistant/components/advantage_air/sensor.py +++ b/homeassistant/components/advantage_air/sensor.py @@ -154,6 +154,6 @@ class AdvantageAirZoneTemp(AdvantageAirEntity, SensorEntity): ) @property - def state(self): + def native_value(self): """Return the current value of the measured temperature.""" return self._zone["measuredTemp"] diff --git a/homeassistant/components/canary/sensor.py b/homeassistant/components/canary/sensor.py index 3870cb357ef..1e7747039b8 100644 --- a/homeassistant/components/canary/sensor.py +++ b/homeassistant/components/canary/sensor.py @@ -120,7 +120,7 @@ class CanarySensor(CoordinatorEntity, SensorEntity): "model": device.device_type["name"], "manufacturer": MANUFACTURER, } - self._attr_unit_of_measurement = sensor_type[1] + self._attr_native_unit_of_measurement = sensor_type[1] self._attr_device_class = sensor_type[3] self._attr_icon = sensor_type[2] diff --git a/homeassistant/components/ezviz/sensor.py b/homeassistant/components/ezviz/sensor.py index 42283b52d35..512491a2548 100644 --- a/homeassistant/components/ezviz/sensor.py +++ b/homeassistant/components/ezviz/sensor.py @@ -5,9 +5,10 @@ import logging from pyezviz.constants import SensorType +from homeassistant.components.sensor import SensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity import DeviceInfo, Entity +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -39,7 +40,7 @@ async def async_setup_entry( async_add_entities(sensors) -class EzvizSensor(CoordinatorEntity, Entity): +class EzvizSensor(CoordinatorEntity, SensorEntity): """Representation of a Ezviz sensor.""" coordinator: EzvizDataUpdateCoordinator diff --git a/homeassistant/components/flipr/sensor.py b/homeassistant/components/flipr/sensor.py index 427a668a72b..3a02b7e2f2e 100644 --- a/homeassistant/components/flipr/sensor.py +++ b/homeassistant/components/flipr/sensor.py @@ -1,13 +1,13 @@ """Sensor platform for the Flipr's pool_sensor.""" from datetime import datetime +from homeassistant.components.sensor import SensorEntity from homeassistant.const import ( ATTR_ATTRIBUTION, DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TIMESTAMP, TEMP_CELSIUS, ) -from homeassistant.helpers.entity import Entity from . import FliprEntity from .const import ATTRIBUTION, CONF_FLIPR_ID, DOMAIN @@ -53,7 +53,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities(sensors_list, True) -class FliprSensor(FliprEntity, Entity): +class FliprSensor(FliprEntity, SensorEntity): """Sensor representing FliprSensor data.""" @property @@ -62,7 +62,7 @@ class FliprSensor(FliprEntity, Entity): return f"Flipr {self.flipr_id} {SENSORS[self.info_type]['name']}" @property - def state(self): + def native_value(self): """State of the sensor.""" state = self.coordinator.data[self.info_type] if isinstance(state, datetime): @@ -80,7 +80,7 @@ class FliprSensor(FliprEntity, Entity): return SENSORS[self.info_type]["icon"] @property - def unit_of_measurement(self): + def native_unit_of_measurement(self): """Return unit of measurement.""" return SENSORS[self.info_type]["unit"] diff --git a/homeassistant/components/fritzbox/sensor.py b/homeassistant/components/fritzbox/sensor.py index 01bea17fb3c..56e025cd605 100644 --- a/homeassistant/components/fritzbox/sensor.py +++ b/homeassistant/components/fritzbox/sensor.py @@ -136,7 +136,7 @@ class FritzBoxPowerSensor(FritzBoxSensor): """The entity class for FRITZ!SmartHome power consumption sensors.""" @property - def state(self) -> float | None: + def native_value(self) -> float | None: """Return the state of the sensor.""" if power := self.device.power: return power / 1000 # type: ignore [no-any-return] @@ -147,7 +147,7 @@ class FritzBoxEnergySensor(FritzBoxSensor): """The entity class for FRITZ!SmartHome total energy sensors.""" @property - def state(self) -> float | None: + def native_value(self) -> float | None: """Return the state of the sensor.""" if energy := self.device.energy: return energy / 1000 # type: ignore [no-any-return] diff --git a/homeassistant/components/fronius/sensor.py b/homeassistant/components/fronius/sensor.py index 68430684d85..5141c79f31b 100644 --- a/homeassistant/components/fronius/sensor.py +++ b/homeassistant/components/fronius/sensor.py @@ -306,9 +306,9 @@ class FroniusTemplateSensor(SensorEntity): async def async_update(self): """Update the internal state.""" state = self._parent.data.get(self._key) - self._attr_state = state.get("value") - if isinstance(self._attr_state, float): - self._attr_native_value = round(self._attr_state, 2) + self._attr_native_value = state.get("value") + if isinstance(self._attr_native_value, float): + self._attr_native_value = round(self._attr_native_value, 2) self._attr_native_unit_of_measurement = state.get("unit") @property diff --git a/homeassistant/components/mill/sensor.py b/homeassistant/components/mill/sensor.py index bdd1a90fb38..a8b4554139f 100644 --- a/homeassistant/components/mill/sensor.py +++ b/homeassistant/components/mill/sensor.py @@ -36,7 +36,7 @@ class MillHeaterEnergySensor(SensorEntity): self._attr_device_class = DEVICE_CLASS_ENERGY self._attr_name = f"{heater.name} {sensor_type.replace('_', ' ')}" self._attr_unique_id = f"{heater.device_id}_{sensor_type}" - self._attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR + self._attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR self._attr_state_class = STATE_CLASS_MEASUREMENT self._attr_device_info = { "identifiers": {(DOMAIN, heater.device_id)}, @@ -67,7 +67,7 @@ class MillHeaterEnergySensor(SensorEntity): else: _state = None if _state is None: - self._attr_state = _state + self._attr_native_value = _state return if self.state is not None and _state < self.state: @@ -81,4 +81,4 @@ class MillHeaterEnergySensor(SensorEntity): month=1, day=1, hour=0, minute=0, second=0, microsecond=0 ) ) - self._attr_state = _state + self._attr_native_value = _state diff --git a/homeassistant/components/powerwall/sensor.py b/homeassistant/components/powerwall/sensor.py index 96542dc3929..0ffa333181d 100644 --- a/homeassistant/components/powerwall/sensor.py +++ b/homeassistant/components/powerwall/sensor.py @@ -152,7 +152,7 @@ class PowerWallEnergyDirectionSensor(PowerWallEntity, SensorEntity): """Representation of an Powerwall Direction Energy sensor.""" _attr_state_class = STATE_CLASS_MEASUREMENT - _attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR + _attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR _attr_device_class = DEVICE_CLASS_ENERGY _attr_last_reset = dt_util.utc_from_timestamp(0) @@ -180,7 +180,7 @@ class PowerWallEnergyDirectionSensor(PowerWallEntity, SensorEntity): ) @property - def state(self): + def native_value(self): """Get the current value in kWh.""" meter = self.coordinator.data[POWERWALL_API_METERS].get_meter(self._meter) if self._meter_direction == _METER_DIRECTION_EXPORT: diff --git a/homeassistant/components/pvpc_hourly_pricing/sensor.py b/homeassistant/components/pvpc_hourly_pricing/sensor.py index 157327fbd19..9cc5603e35b 100644 --- a/homeassistant/components/pvpc_hourly_pricing/sensor.py +++ b/homeassistant/components/pvpc_hourly_pricing/sensor.py @@ -7,7 +7,7 @@ from typing import Any from aiopvpc import PVPCData -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_NAME, CURRENCY_EURO, ENERGY_KILO_WATT_HOUR from homeassistant.core import HomeAssistant, callback @@ -51,9 +51,10 @@ async def async_setup_entry( class ElecPriceSensor(RestoreEntity, SensorEntity): """Class to hold the prices of electricity as a sensor.""" - unit_of_measurement = UNIT - icon = ICON - should_poll = False + _attr_icon = ICON + _attr_native_unit_of_measurement = UNIT + _attr_should_poll = False + _attr_state_class = STATE_CLASS_MEASUREMENT def __init__(self, name, unique_id, pvpc_data_handler): """Initialize the sensor object.""" diff --git a/homeassistant/components/renault/sensor.py b/homeassistant/components/renault/sensor.py index 8403a04d001..51f38d6a4d6 100644 --- a/homeassistant/components/renault/sensor.py +++ b/homeassistant/components/renault/sensor.py @@ -88,10 +88,10 @@ class RenaultBatteryAutonomySensor(RenaultBatteryDataEntity, SensorEntity): """Battery autonomy sensor.""" _attr_icon = "mdi:ev-station" - _attr_unit_of_measurement = LENGTH_KILOMETERS + _attr_native_unit_of_measurement = LENGTH_KILOMETERS @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return self.data.batteryAutonomy if self.data else None @@ -100,10 +100,10 @@ class RenaultBatteryLevelSensor(RenaultBatteryDataEntity, SensorEntity): """Battery Level sensor.""" _attr_device_class = DEVICE_CLASS_BATTERY - _attr_unit_of_measurement = PERCENTAGE + _attr_native_unit_of_measurement = PERCENTAGE @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return self.data.batteryLevel if self.data else None @@ -128,10 +128,10 @@ class RenaultBatteryTemperatureSensor(RenaultBatteryDataEntity, SensorEntity): """Battery Temperature sensor.""" _attr_device_class = DEVICE_CLASS_TEMPERATURE - _attr_unit_of_measurement = TEMP_CELSIUS + _attr_native_unit_of_measurement = TEMP_CELSIUS @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return self.data.batteryTemperature if self.data else None @@ -142,7 +142,7 @@ class RenaultChargeModeSensor(RenaultChargeModeDataEntity, SensorEntity): _attr_device_class = DEVICE_CLASS_CHARGE_MODE @property - def state(self) -> str | None: + def native_value(self) -> str | None: """Return the state of this entity.""" return self.data.chargeMode if self.data else None @@ -160,7 +160,7 @@ class RenaultChargeStateSensor(RenaultBatteryDataEntity, SensorEntity): _attr_device_class = DEVICE_CLASS_CHARGE_STATE @property - def state(self) -> str | None: + def native_value(self) -> str | None: """Return the state of this entity.""" charging_status = self.data.get_charging_status() if self.data else None return slugify(charging_status.name) if charging_status is not None else None @@ -175,10 +175,10 @@ class RenaultChargingRemainingTimeSensor(RenaultBatteryDataEntity, SensorEntity) """Charging Remaining Time sensor.""" _attr_icon = "mdi:timer" - _attr_unit_of_measurement = TIME_MINUTES + _attr_native_unit_of_measurement = TIME_MINUTES @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return self.data.chargingRemainingTime if self.data else None @@ -187,10 +187,10 @@ class RenaultChargingPowerSensor(RenaultBatteryDataEntity, SensorEntity): """Charging Power sensor.""" _attr_device_class = DEVICE_CLASS_ENERGY - _attr_unit_of_measurement = POWER_KILO_WATT + _attr_native_unit_of_measurement = POWER_KILO_WATT @property - def state(self) -> float | None: + def native_value(self) -> float | None: """Return the state of this entity.""" if not self.data or self.data.chargingInstantaneousPower is None: return None @@ -204,10 +204,10 @@ class RenaultFuelAutonomySensor(RenaultCockpitDataEntity, SensorEntity): """Fuel autonomy sensor.""" _attr_icon = "mdi:gas-station" - _attr_unit_of_measurement = LENGTH_KILOMETERS + _attr_native_unit_of_measurement = LENGTH_KILOMETERS @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return ( round(self.data.fuelAutonomy) @@ -220,10 +220,10 @@ class RenaultFuelQuantitySensor(RenaultCockpitDataEntity, SensorEntity): """Fuel quantity sensor.""" _attr_icon = "mdi:fuel" - _attr_unit_of_measurement = VOLUME_LITERS + _attr_native_unit_of_measurement = VOLUME_LITERS @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return ( round(self.data.fuelQuantity) @@ -236,10 +236,10 @@ class RenaultMileageSensor(RenaultCockpitDataEntity, SensorEntity): """Mileage sensor.""" _attr_icon = "mdi:sign-direction" - _attr_unit_of_measurement = LENGTH_KILOMETERS + _attr_native_unit_of_measurement = LENGTH_KILOMETERS @property - def state(self) -> int | None: + def native_value(self) -> int | None: """Return the state of this entity.""" return ( round(self.data.totalMileage) @@ -252,10 +252,10 @@ class RenaultOutsideTemperatureSensor(RenaultHVACDataEntity, SensorEntity): """HVAC Outside Temperature sensor.""" _attr_device_class = DEVICE_CLASS_TEMPERATURE - _attr_unit_of_measurement = TEMP_CELSIUS + _attr_native_unit_of_measurement = TEMP_CELSIUS @property - def state(self) -> float | None: + def native_value(self) -> float | None: """Return the state of this entity.""" return self.data.externalTemperature if self.data else None @@ -266,7 +266,7 @@ class RenaultPlugStateSensor(RenaultBatteryDataEntity, SensorEntity): _attr_device_class = DEVICE_CLASS_PLUG_STATE @property - def state(self) -> str | None: + def native_value(self) -> str | None: """Return the state of this entity.""" plug_status = self.data.get_plug_status() if self.data else None return slugify(plug_status.name) if plug_status is not None else None diff --git a/homeassistant/components/smartthings/sensor.py b/homeassistant/components/smartthings/sensor.py index c4d84ec5f69..a8e6c0472e9 100644 --- a/homeassistant/components/smartthings/sensor.py +++ b/homeassistant/components/smartthings/sensor.py @@ -568,7 +568,7 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity): return f"{self._device.device_id}.{self.report_name}" @property - def state(self): + def native_value(self): """Return the state of the sensor.""" value = self._device.status.attributes[Attribute.power_consumption].value if value is None or value.get(self.report_name) is None: @@ -585,7 +585,7 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity): return DEVICE_CLASS_ENERGY @property - def unit_of_measurement(self): + def native_unit_of_measurement(self): """Return the unit this state is expressed in.""" if self.report_name == "power": return POWER_WATT diff --git a/homeassistant/components/spider/sensor.py b/homeassistant/components/spider/sensor.py index 998a9ff8eee..bf1ab0b18be 100644 --- a/homeassistant/components/spider/sensor.py +++ b/homeassistant/components/spider/sensor.py @@ -29,7 +29,7 @@ async def async_setup_entry(hass, config, async_add_entities): class SpiderPowerPlugEnergy(SensorEntity): """Representation of a Spider Power Plug (energy).""" - _attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR + _attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR _attr_device_class = DEVICE_CLASS_ENERGY _attr_state_class = STATE_CLASS_MEASUREMENT @@ -59,7 +59,7 @@ class SpiderPowerPlugEnergy(SensorEntity): return f"{self.power_plug.name} Total Energy Today" @property - def state(self) -> float: + def native_value(self) -> float: """Return todays energy usage in Kwh.""" return round(self.power_plug.today_energy_consumption / 1000, 2) @@ -80,7 +80,7 @@ class SpiderPowerPlugPower(SensorEntity): _attr_device_class = DEVICE_CLASS_POWER _attr_state_class = STATE_CLASS_MEASUREMENT - _attr_unit_of_measurement = POWER_WATT + _attr_native_unit_of_measurement = POWER_WATT def __init__(self, api, power_plug) -> None: """Initialize the Spider Power Plug.""" @@ -108,7 +108,7 @@ class SpiderPowerPlugPower(SensorEntity): return f"{self.power_plug.name} Power Consumption" @property - def state(self) -> float: + def native_value(self) -> float: """Return the current power usage in W.""" return round(self.power_plug.current_energy_consumption) diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index 697641915f7..fae7939cd65 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -137,7 +137,7 @@ class SmartPlugSensor(CoordinatorEntity, SensorEntity): return self.coordinator.data @property - def state(self) -> float | None: + def native_value(self) -> float | None: """Return the sensors state.""" return self.data[CONF_EMETER_PARAMS][self.entity_description.key] diff --git a/homeassistant/components/wemo/sensor.py b/homeassistant/components/wemo/sensor.py index ebd68231e0c..b9d22e6995a 100644 --- a/homeassistant/components/wemo/sensor.py +++ b/homeassistant/components/wemo/sensor.py @@ -98,7 +98,7 @@ class InsightCurrentPower(InsightSensor): ) @property - def state(self) -> StateType: + def native_value(self) -> StateType: """Return the current power consumption.""" return ( convert(self.wemo.insight_params[self.entity_description.key], float, 0.0) @@ -123,7 +123,7 @@ class InsightTodayEnergy(InsightSensor): return dt.start_of_local_day() @property - def state(self) -> StateType: + def native_value(self) -> StateType: """Return the current energy use today.""" miliwatts = convert( self.wemo.insight_params[self.entity_description.key], float, 0.0 diff --git a/homeassistant/components/xiaomi_miio/sensor.py b/homeassistant/components/xiaomi_miio/sensor.py index bbf83825dca..aff0b5212f1 100644 --- a/homeassistant/components/xiaomi_miio/sensor.py +++ b/homeassistant/components/xiaomi_miio/sensor.py @@ -280,7 +280,7 @@ class XiaomiGenericSensor(XiaomiCoordinatedMiioEntity, SensorEntity): self.entity_description = description @property - def state(self): + def native_value(self): """Return the state of the device.""" self._state = self._extract_value_from_attribute( self.coordinator.data, self.entity_description.key diff --git a/homeassistant/components/youless/sensor.py b/homeassistant/components/youless/sensor.py index 54155034919..bc0f1ee873b 100644 --- a/homeassistant/components/youless/sensor.py +++ b/homeassistant/components/youless/sensor.py @@ -3,11 +3,11 @@ from __future__ import annotations from youless_api.youless_sensor import YoulessSensor +from homeassistant.components.sensor import SensorEntity from homeassistant.components.youless import DOMAIN from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_DEVICE, DEVICE_CLASS_POWER from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import ( @@ -40,7 +40,7 @@ async def async_setup_entry( ) -class YoulessBaseSensor(CoordinatorEntity, Entity): +class YoulessBaseSensor(CoordinatorEntity, SensorEntity): """The base sensor for Youless.""" def __init__( @@ -71,7 +71,7 @@ class YoulessBaseSensor(CoordinatorEntity, Entity): return None @property - def unit_of_measurement(self) -> str | None: + def native_unit_of_measurement(self) -> str | None: """Return the unit of measurement for the sensor.""" if self.get_sensor is None: return None @@ -79,7 +79,7 @@ class YoulessBaseSensor(CoordinatorEntity, Entity): return self.get_sensor.unit_of_measurement @property - def state(self) -> StateType: + def native_value(self) -> StateType: """Determine the state value, only if a sensor is initialized.""" if self.get_sensor is None: return None