Fix some sensor classes (#48254)

* Fix some sensor classes

* Tweak

* Tweak
pull/48292/head
Erik Montnemery 2021-03-23 15:56:33 +01:00 committed by GitHub
parent 6932cf9534
commit a09c8eecb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 88 additions and 61 deletions

View File

@ -1,4 +1,5 @@
"""Abstraction form AEMET OpenData sensors."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -6,7 +7,7 @@ from .const import ATTRIBUTION, SENSOR_DEVICE_CLASS, SENSOR_NAME, SENSOR_UNIT
from .weather_update_coordinator import WeatherUpdateCoordinator
class AbstractAemetSensor(CoordinatorEntity):
class AbstractAemetSensor(CoordinatorEntity, SensorEntity):
"""Abstract class for an AEMET OpenData sensor."""
def __init__(

View File

@ -143,13 +143,6 @@ class HydrawiseEntity(Entity):
"""Call update method."""
self.async_schedule_update_ha_state(True)
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return DEVICE_MAP[self._sensor_type][
DEVICE_MAP_INDEX.index("UNIT_OF_MEASURE_INDEX")
]
@property
def extra_state_attributes(self):
"""Return the state attributes."""

View File

@ -8,7 +8,7 @@ from homeassistant.const import CONF_MONITORED_CONDITIONS
import homeassistant.helpers.config_validation as cv
from homeassistant.util import dt
from . import DATA_HYDRAWISE, SENSORS, HydrawiseEntity
from . import DATA_HYDRAWISE, DEVICE_MAP, DEVICE_MAP_INDEX, SENSORS, HydrawiseEntity
_LOGGER = logging.getLogger(__name__)
@ -44,6 +44,13 @@ class HydrawiseSensor(HydrawiseEntity, SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return DEVICE_MAP[self._sensor_type][
DEVICE_MAP_INDEX.index("UNIT_OF_MEASURE_INDEX")
]
def update(self):
"""Get the latest data and updates the states."""
mydata = self.hass.data[DATA_HYDRAWISE].data

View File

@ -6,6 +6,7 @@ from functools import partial
from pyiqvia import Client
from pyiqvia.errors import IQVIAError
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client
@ -109,7 +110,7 @@ async def async_unload_entry(hass, entry):
return unload_ok
class IQVIAEntity(CoordinatorEntity):
class IQVIAEntity(CoordinatorEntity, SensorEntity):
"""Define a base IQVIA entity."""
def __init__(self, coordinator, entry, sensor_type, name, icon):

View File

@ -3,7 +3,6 @@ from statistics import mean
import numpy as np
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_STATE
from homeassistant.core import callback
@ -99,7 +98,7 @@ def calculate_trend(indices):
return TREND_FLAT
class ForecastSensor(IQVIAEntity, SensorEntity):
class ForecastSensor(IQVIAEntity):
"""Define sensor related to forecast data."""
@callback
@ -138,7 +137,7 @@ class ForecastSensor(IQVIAEntity, SensorEntity):
self._state = average
class IndexSensor(IQVIAEntity, SensorEntity):
class IndexSensor(IQVIAEntity):
"""Define sensor related to indices."""
@callback

View File

@ -363,11 +363,6 @@ class NestSensorDevice(Entity):
"""Return the name of the nest, if any."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit
@property
def should_poll(self):
"""Do not need poll thanks using Nest streaming API."""

View File

@ -153,6 +153,11 @@ async def async_setup_legacy_entry(hass, entry, async_add_entities):
class NestBasicSensor(NestSensorDevice, SensorEntity):
"""Representation a basic Nest sensor."""
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit
@property
def state(self):
"""Return the state of the sensor."""
@ -188,6 +193,11 @@ class NestTempSensor(NestSensorDevice, SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit
@property
def device_class(self):
"""Return the device class of the sensor."""

View File

@ -7,6 +7,7 @@ from google_nest_sdm.device import Device
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
from google_nest_sdm.exceptions import GoogleNestException
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
DEVICE_CLASS_HUMIDITY,
@ -15,7 +16,6 @@ from homeassistant.const import (
TEMP_CELSIUS,
)
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import HomeAssistantType
from .const import DATA_SUBSCRIBER, DOMAIN
@ -53,7 +53,7 @@ async def async_setup_sdm_entry(
async_add_entities(entities)
class SensorBase(Entity):
class SensorBase(SensorEntity):
"""Representation of a dynamically updated Sensor."""
def __init__(self, device: Device):

View File

@ -54,11 +54,6 @@ class OneWireBaseEntity(Entity):
"""Return the class of this device."""
return self._device_class
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes of the entity."""

View File

@ -1,4 +1,6 @@
"""Support for 1-Wire environment sensors."""
from __future__ import annotations
from glob import glob
import logging
import os
@ -394,7 +396,16 @@ def get_entities(onewirehub: OneWireHub, config):
return entities
class OneWireProxySensor(OneWireProxyEntity, SensorEntity):
class OneWireSensor(OneWireBaseEntity, SensorEntity):
"""Mixin for sensor specific attributes."""
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
class OneWireProxySensor(OneWireProxyEntity, OneWireSensor):
"""Implementation of a 1-Wire sensor connected through owserver."""
@property
@ -403,7 +414,7 @@ class OneWireProxySensor(OneWireProxyEntity, SensorEntity):
return self._state
class OneWireDirectSensor(OneWireBaseEntity, SensorEntity):
class OneWireDirectSensor(OneWireSensor):
"""Implementation of a 1-Wire sensor directly connected to RPI GPIO."""
def __init__(self, name, device_file, device_info, owsensor):
@ -431,7 +442,7 @@ class OneWireDirectSensor(OneWireBaseEntity, SensorEntity):
self._state = value
class OneWireOWFSSensor(OneWireBaseEntity, SensorEntity): # pragma: no cover
class OneWireOWFSSensor(OneWireSensor): # pragma: no cover
"""Implementation of a 1-Wire sensor through owfs.
This part of the implementation does not conform to policy regarding 3rd-party libraries, and will not longer be updated.

View File

@ -160,11 +160,6 @@ class RainCloudEntity(Entity):
"""Call update method."""
self.schedule_update_ha_state(True)
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return UNIT_OF_MEASUREMENT_MAP.get(self._sensor_type)
@property
def extra_state_attributes(self):
"""Return the state attributes."""

View File

@ -8,7 +8,13 @@ from homeassistant.const import CONF_MONITORED_CONDITIONS
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.icon import icon_for_battery_level
from . import DATA_RAINCLOUD, ICON_MAP, SENSORS, RainCloudEntity
from . import (
DATA_RAINCLOUD,
ICON_MAP,
SENSORS,
UNIT_OF_MEASUREMENT_MAP,
RainCloudEntity,
)
_LOGGER = logging.getLogger(__name__)
@ -46,6 +52,11 @@ class RainCloudSensor(RainCloudEntity, SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return UNIT_OF_MEASUREMENT_MAP.get(self._sensor_type)
def update(self):
"""Get the latest data and updates the states."""
_LOGGER.debug("Updating RainCloud sensor: %s", self._name)

View File

@ -267,11 +267,6 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
return self.description.value(value)
@property
def unit_of_measurement(self):
"""Return unit of sensor."""
return self._unit
@property
def device_class(self):
"""Device class of sensor."""
@ -348,11 +343,6 @@ class ShellyRestAttributeEntity(update_coordinator.CoordinatorEntity):
)
return self._last_value
@property
def unit_of_measurement(self):
"""Return unit of sensor."""
return self.description.unit
@property
def device_class(self):
"""Device class of sensor."""

View File

@ -212,6 +212,11 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
"""Return value of sensor."""
return self.attribute_value
@property
def unit_of_measurement(self):
"""Return unit of sensor."""
return self._unit
class ShellyRestSensor(ShellyRestAttributeEntity, SensorEntity):
"""Represent a shelly REST sensor."""
@ -221,6 +226,11 @@ class ShellyRestSensor(ShellyRestAttributeEntity, SensorEntity):
"""Return value of sensor."""
return self.attribute_value
@property
def unit_of_measurement(self):
"""Return unit of sensor."""
return self.description.unit
class ShellySleepingSensor(ShellySleepingBlockAttributeEntity, SensorEntity):
"""Represent a shelly sleeping sensor."""
@ -232,3 +242,8 @@ class ShellySleepingSensor(ShellySleepingBlockAttributeEntity, SensorEntity):
return self.attribute_value
return self.last_state
@property
def unit_of_measurement(self):
"""Return unit of sensor."""
return self._unit

View File

@ -72,7 +72,6 @@ from .const import (
STORAGE_VOL_SENSORS,
SYNO_API,
SYSTEM_LOADED,
TEMP_SENSORS_KEYS,
UNDO_UPDATE_LISTENER,
UTILISATION_SENSORS,
)
@ -631,13 +630,6 @@ class SynologyDSMBaseEntity(CoordinatorEntity):
"""Return the icon."""
return self._icon
@property
def unit_of_measurement(self) -> str:
"""Return the unit the value is expressed in."""
if self.entity_type in TEMP_SENSORS_KEYS:
return self.hass.config.units.temperature_unit
return self._unit
@property
def device_class(self) -> str:
"""Return the class of this device."""

View File

@ -87,7 +87,18 @@ async def async_setup_entry(
async_add_entities(entities)
class SynoDSMUtilSensor(SynologyDSMBaseEntity, SensorEntity):
class SynoDSMSensor(SynologyDSMBaseEntity):
"""Mixin for sensor specific attributes."""
@property
def unit_of_measurement(self) -> str:
"""Return the unit the value is expressed in."""
if self.entity_type in TEMP_SENSORS_KEYS:
return self.hass.config.units.temperature_unit
return self._unit
class SynoDSMUtilSensor(SynoDSMSensor, SensorEntity):
"""Representation a Synology Utilisation sensor."""
@property
@ -119,7 +130,7 @@ class SynoDSMUtilSensor(SynologyDSMBaseEntity, SensorEntity):
return bool(self._api.utilisation)
class SynoDSMStorageSensor(SynologyDSMDeviceEntity, SensorEntity):
class SynoDSMStorageSensor(SynologyDSMDeviceEntity, SynoDSMSensor, SensorEntity):
"""Representation a Synology Storage sensor."""
@property
@ -140,7 +151,7 @@ class SynoDSMStorageSensor(SynologyDSMDeviceEntity, SensorEntity):
return attr
class SynoDSMInfoSensor(SynologyDSMBaseEntity, SensorEntity):
class SynoDSMInfoSensor(SynoDSMSensor, SensorEntity):
"""Representation a Synology information sensor."""
def __init__(

View File

@ -967,11 +967,6 @@ class BaseWithingsSensor(Entity):
"""Return a unique, Home Assistant friendly identifier for this entity."""
return self._unique_id
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity, if any."""
return self._attribute.unit_of_measurement
@property
def icon(self) -> str:
"""Icon to use in the frontend, if any."""

View File

@ -35,3 +35,8 @@ class WithingsHealthSensor(BaseWithingsSensor, SensorEntity):
def state(self) -> None | str | int | float:
"""Return the state of the entity."""
return self._state_data
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement of this entity, if any."""
return self._attribute.unit_of_measurement

View File

@ -9,6 +9,7 @@ import xbee_helper.const as xb_const
from xbee_helper.device import convert_adc
from xbee_helper.exceptions import ZigBeeException, ZigBeeTxFailure
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (
CONF_ADDRESS,
CONF_DEVICE,
@ -365,7 +366,7 @@ class XBeeDigitalOut(XBeeDigitalIn):
self._state = self._config.state2bool[pin_state]
class XBeeAnalogIn(Entity):
class XBeeAnalogIn(SensorEntity):
"""Representation of a GPIO pin configured as an analog input."""
def __init__(self, config, device):