mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
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
This commit is contained in:
parent
a9807e5fa7
commit
87e7a8fb5f
@ -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):
|
||||
|
@ -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"]
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"]
|
||||
|
||||
|
@ -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]
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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."""
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user