Add statistics support to nest sensors (#57393)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
1b71eafeba
commit
d0cc890d2b
2 changed files with 31 additions and 43 deletions
|
@ -7,7 +7,7 @@ from google_nest_sdm.device import Device
|
||||||
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
|
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
|
||||||
from google_nest_sdm.exceptions import GoogleNestException
|
from google_nest_sdm.exceptions import GoogleNestException
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
|
@ -17,7 +17,6 @@ from homeassistant.const import (
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DATA_SUBSCRIBER, DOMAIN
|
from .const import DATA_SUBSCRIBER, DOMAIN
|
||||||
|
@ -58,26 +57,15 @@ async def async_setup_sdm_entry(
|
||||||
class SensorBase(SensorEntity):
|
class SensorBase(SensorEntity):
|
||||||
"""Representation of a dynamically updated Sensor."""
|
"""Representation of a dynamically updated Sensor."""
|
||||||
|
|
||||||
|
_attr_shoud_poll = False
|
||||||
|
_attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
|
|
||||||
def __init__(self, device: Device) -> None:
|
def __init__(self, device: Device) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._device = device
|
self._device = device
|
||||||
self._device_info = NestDeviceInfo(device)
|
self._device_info = NestDeviceInfo(device)
|
||||||
|
self._attr_unique_id = f"{device.name}-{self.device_class}"
|
||||||
@property
|
self._attr_device_info = self._device_info.device_info
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""Disable polling since entities have state pushed via pubsub."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str | None:
|
|
||||||
"""Return a unique ID."""
|
|
||||||
# The API "name" field is a unique device identifier.
|
|
||||||
return f"{self._device.name}-{self.device_class}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return device specific attributes."""
|
|
||||||
return self._device_info.device_info
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Run when entity is added to register update signal handler."""
|
"""Run when entity is added to register update signal handler."""
|
||||||
|
@ -89,6 +77,9 @@ class SensorBase(SensorEntity):
|
||||||
class TemperatureSensor(SensorBase):
|
class TemperatureSensor(SensorBase):
|
||||||
"""Representation of a Temperature Sensor."""
|
"""Representation of a Temperature Sensor."""
|
||||||
|
|
||||||
|
_attr_device_class = DEVICE_CLASS_TEMPERATURE
|
||||||
|
_attr_native_unit_of_measurement = TEMP_CELSIUS
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
|
@ -100,25 +91,12 @@ class TemperatureSensor(SensorBase):
|
||||||
trait: TemperatureTrait = self._device.traits[TemperatureTrait.NAME]
|
trait: TemperatureTrait = self._device.traits[TemperatureTrait.NAME]
|
||||||
return trait.ambient_temperature_celsius
|
return trait.ambient_temperature_celsius
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return TEMP_CELSIUS
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str:
|
|
||||||
"""Return the class of this device."""
|
|
||||||
return DEVICE_CLASS_TEMPERATURE
|
|
||||||
|
|
||||||
|
|
||||||
class HumiditySensor(SensorBase):
|
class HumiditySensor(SensorBase):
|
||||||
"""Representation of a Humidity Sensor."""
|
"""Representation of a Humidity Sensor."""
|
||||||
|
|
||||||
@property
|
_attr_device_class = DEVICE_CLASS_HUMIDITY
|
||||||
def unique_id(self) -> str | None:
|
_attr_native_unit_of_measurement = PERCENTAGE
|
||||||
"""Return a unique ID."""
|
|
||||||
# The API returns the identifier under the name field.
|
|
||||||
return f"{self._device.name}-humidity"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
@ -130,13 +108,3 @@ class HumiditySensor(SensorBase):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
trait: HumidityTrait = self._device.traits[HumidityTrait.NAME]
|
trait: HumidityTrait = self._device.traits[HumidityTrait.NAME]
|
||||||
return trait.ambient_humidity_percent
|
return trait.ambient_humidity_percent
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return PERCENTAGE
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str:
|
|
||||||
"""Return the class of this device."""
|
|
||||||
return DEVICE_CLASS_HUMIDITY
|
|
||||||
|
|
|
@ -8,6 +8,15 @@ pubsub subscriber.
|
||||||
from google_nest_sdm.device import Device
|
from google_nest_sdm.device import Device
|
||||||
from google_nest_sdm.event import EventMessage
|
from google_nest_sdm.event import EventMessage
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT
|
||||||
|
from homeassistant.const import (
|
||||||
|
ATTR_DEVICE_CLASS,
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
|
DEVICE_CLASS_HUMIDITY,
|
||||||
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
|
PERCENTAGE,
|
||||||
|
TEMP_CELSIUS,
|
||||||
|
)
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
|
|
||||||
from .common import async_setup_sdm_platform
|
from .common import async_setup_sdm_platform
|
||||||
|
@ -49,10 +58,16 @@ async def test_thermostat_device(hass):
|
||||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||||
assert temperature is not None
|
assert temperature is not None
|
||||||
assert temperature.state == "25.1"
|
assert temperature.state == "25.1"
|
||||||
|
assert temperature.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
|
||||||
|
assert temperature.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TEMPERATURE
|
||||||
|
assert temperature.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
|
||||||
|
|
||||||
humidity = hass.states.get("sensor.my_sensor_humidity")
|
humidity = hass.states.get("sensor.my_sensor_humidity")
|
||||||
assert humidity is not None
|
assert humidity is not None
|
||||||
assert humidity.state == "35.0"
|
assert humidity.state == "35.0"
|
||||||
|
assert humidity.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
|
||||||
|
assert humidity.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_HUMIDITY
|
||||||
|
assert humidity.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
|
||||||
|
|
||||||
registry = er.async_get(hass)
|
registry = er.async_get(hass)
|
||||||
entry = registry.async_get("sensor.my_sensor_temperature")
|
entry = registry.async_get("sensor.my_sensor_temperature")
|
||||||
|
@ -60,6 +75,11 @@ async def test_thermostat_device(hass):
|
||||||
assert entry.original_name == "My Sensor Temperature"
|
assert entry.original_name == "My Sensor Temperature"
|
||||||
assert entry.domain == "sensor"
|
assert entry.domain == "sensor"
|
||||||
|
|
||||||
|
entry = registry.async_get("sensor.my_sensor_humidity")
|
||||||
|
assert entry.unique_id == "some-device-id-humidity"
|
||||||
|
assert entry.original_name == "My Sensor Humidity"
|
||||||
|
assert entry.domain == "sensor"
|
||||||
|
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
device = device_registry.async_get(entry.device_id)
|
device = device_registry.async_get(entry.device_id)
|
||||||
assert device.name == "My Sensor"
|
assert device.name == "My Sensor"
|
||||||
|
|
Loading…
Add table
Reference in a new issue