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
|
2020-10-27 14:20:01 +00:00
|
|
|
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
|
2020-11-19 11:26:49 +00:00
|
|
|
from google_nest_sdm.exceptions import GoogleNestException
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-03-23 14:56:33 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-10-21 08:17:49 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
PERCENTAGE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2020-11-19 11:26:49 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2020-10-21 08:17:49 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
|
2020-12-27 08:49:22 +00:00
|
|
|
from .const import DATA_SUBSCRIBER, DOMAIN
|
2020-10-27 14:20:01 +00:00
|
|
|
from .device_info import DeviceInfo
|
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(
|
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensors."""
|
|
|
|
|
2020-11-30 08:19:42 +00:00
|
|
|
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
|
2020-11-19 11:26:49 +00:00
|
|
|
try:
|
|
|
|
device_manager = await subscriber.async_get_device_manager()
|
|
|
|
except GoogleNestException as err:
|
|
|
|
_LOGGER.warning("Failed to get devices: %s", err)
|
|
|
|
raise PlatformNotReady from err
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
entities = []
|
|
|
|
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."""
|
|
|
|
|
|
|
|
def __init__(self, device: Device):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._device = device
|
2020-10-27 14:20:01 +00:00
|
|
|
self._device_info = DeviceInfo(device)
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
@property
|
2020-10-24 18:48:28 +00:00
|
|
|
def should_poll(self) -> bool:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Disable polling since entities have state pushed via pubsub."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""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):
|
|
|
|
"""Return device specific attributes."""
|
2020-10-27 14:20:01 +00:00
|
|
|
return self._device_info.device_info
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""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."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2020-10-27 14:20:01 +00:00
|
|
|
return f"{self._device_info.device_name} Temperature"
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
trait = self._device.traits[TemperatureTrait.NAME]
|
|
|
|
return trait.ambient_temperature_celsius
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device."""
|
|
|
|
return DEVICE_CLASS_TEMPERATURE
|
|
|
|
|
|
|
|
|
|
|
|
class HumiditySensor(SensorBase):
|
|
|
|
"""Representation of a Humidity Sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-03-18 12:21:46 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Return a unique ID."""
|
|
|
|
# The API returns the identifier under the name field.
|
|
|
|
return f"{self._device.name}-humidity"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2020-10-27 14:20:01 +00:00
|
|
|
return f"{self._device_info.device_name} Humidity"
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
trait = self._device.traits[HumidityTrait.NAME]
|
|
|
|
return trait.ambient_humidity_percent
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return PERCENTAGE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device."""
|
|
|
|
return DEVICE_CLASS_HUMIDITY
|