2020-10-21 08:17:49 +00:00
|
|
|
"""Support for Google Nest SDM sensors."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2020-11-19 11:26:49 +00:00
|
|
|
import logging
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
from google_nest_sdm.device import Device
|
2022-05-20 14:47:18 +00:00
|
|
|
from google_nest_sdm.device_manager import DeviceManager
|
2020-10-27 14:20:01 +00:00
|
|
|
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-12-15 21:00:10 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorStateClass,
|
2020-10-21 08:17:49 +00:00
|
|
|
)
|
2021-12-15 21:00:10 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-20 17:39:09 +00:00
|
|
|
from homeassistant.const import PERCENTAGE, UnitOfTemperature
|
2021-04-22 18:23:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-26 23:43:52 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2022-05-20 14:47:18 +00:00
|
|
|
from .const import DATA_DEVICE_MANAGER, DOMAIN
|
2021-07-26 23:43:52 +00:00
|
|
|
from .device_info import NestDeviceInfo
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2020-11-19 11:26:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-10-24 18:48:28 +00:00
|
|
|
DEVICE_TYPE_MAP = {
|
|
|
|
"sdm.devices.types.CAMERA": "Camera",
|
|
|
|
"sdm.devices.types.DISPLAY": "Display",
|
|
|
|
"sdm.devices.types.DOORBELL": "Doorbell",
|
|
|
|
"sdm.devices.types.THERMOSTAT": "Thermostat",
|
|
|
|
}
|
|
|
|
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
async def async_setup_sdm_entry(
|
2021-07-26 23:43:52 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-10-21 08:17:49 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the sensors."""
|
|
|
|
|
2022-06-21 14:36:13 +00:00
|
|
|
device_manager: DeviceManager = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
DATA_DEVICE_MANAGER
|
|
|
|
]
|
2021-07-20 05:59:31 +00:00
|
|
|
entities: list[SensorEntity] = []
|
2020-10-21 08:17:49 +00:00
|
|
|
for device in device_manager.devices.values():
|
|
|
|
if TemperatureTrait.NAME in device.traits:
|
|
|
|
entities.append(TemperatureSensor(device))
|
|
|
|
if HumidityTrait.NAME in device.traits:
|
|
|
|
entities.append(HumiditySensor(device))
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2021-03-23 14:56:33 +00:00
|
|
|
class SensorBase(SensorEntity):
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Representation of a dynamically updated Sensor."""
|
|
|
|
|
2022-01-20 01:49:52 +00:00
|
|
|
_attr_should_poll = False
|
2021-12-15 21:00:10 +00:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2022-07-09 17:21:53 +00:00
|
|
|
_attr_has_entity_name = True
|
2021-10-12 08:56:57 +00:00
|
|
|
|
2021-05-20 12:06:44 +00:00
|
|
|
def __init__(self, device: Device) -> None:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._device = device
|
2021-07-26 23:43:52 +00:00
|
|
|
self._device_info = NestDeviceInfo(device)
|
2021-10-12 08:56:57 +00:00
|
|
|
self._attr_unique_id = f"{device.name}-{self.device_class}"
|
|
|
|
self._attr_device_info = self._device_info.device_info
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2022-09-29 02:23:11 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return the device availability."""
|
|
|
|
return self._device_info.available
|
|
|
|
|
2021-07-26 23:43:52 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Run when entity is added to register update signal handler."""
|
|
|
|
self.async_on_remove(
|
2020-12-27 08:49:22 +00:00
|
|
|
self._device.add_update_listener(self.async_write_ha_state)
|
2020-10-21 08:17:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TemperatureSensor(SensorBase):
|
|
|
|
"""Representation of a Temperature Sensor."""
|
|
|
|
|
2021-12-15 21:00:10 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.TEMPERATURE
|
2022-12-20 17:39:09 +00:00
|
|
|
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
|
2022-07-09 17:21:53 +00:00
|
|
|
_attr_name = "Temperature"
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self) -> float:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-07-26 23:43:52 +00:00
|
|
|
trait: TemperatureTrait = self._device.traits[TemperatureTrait.NAME]
|
2021-10-23 03:31:25 +00:00
|
|
|
# Round for display purposes because the API returns 5 decimal places.
|
|
|
|
# This can be removed if the SDM API issue is fixed, or a frontend
|
|
|
|
# display fix is added for all integrations.
|
|
|
|
return float(round(trait.ambient_temperature_celsius, 1))
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HumiditySensor(SensorBase):
|
|
|
|
"""Representation of a Humidity Sensor."""
|
|
|
|
|
2021-12-15 21:00:10 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.HUMIDITY
|
2021-10-12 08:56:57 +00:00
|
|
|
_attr_native_unit_of_measurement = PERCENTAGE
|
2022-07-09 17:21:53 +00:00
|
|
|
_attr_name = "Humidity"
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
@property
|
2021-10-23 03:31:25 +00:00
|
|
|
def native_value(self) -> int:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-07-26 23:43:52 +00:00
|
|
|
trait: HumidityTrait = self._device.traits[HumidityTrait.NAME]
|
2021-10-23 03:31:25 +00:00
|
|
|
# Cast without loss of precision because the API always returns an integer.
|
|
|
|
return int(trait.ambient_humidity_percent)
|