core/homeassistant/components/simplisafe/sensor.py

64 lines
2.0 KiB
Python

"""Support for SimpliSafe freeze sensor."""
from __future__ import annotations
from simplipy.device import DeviceTypes
from simplipy.device.sensor.v3 import SensorV3
from simplipy.system.v3 import SystemV3
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SimpliSafe
from .const import DOMAIN, LOGGER
from .entity import SimpliSafeEntity
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up SimpliSafe freeze sensors based on a config entry."""
simplisafe = hass.data[DOMAIN][entry.entry_id]
sensors: list[SimplisafeFreezeSensor] = []
for system in simplisafe.systems.values():
if system.version == 2:
LOGGER.warning("Skipping sensor setup for V2 system: %s", system.system_id)
continue
sensors.extend(
SimplisafeFreezeSensor(simplisafe, system, sensor)
for sensor in system.sensors.values()
if sensor.type == DeviceTypes.TEMPERATURE
)
async_add_entities(sensors)
class SimplisafeFreezeSensor(SimpliSafeEntity, SensorEntity):
"""Define a SimpliSafe freeze sensor entity."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = UnitOfTemperature.FAHRENHEIT
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(
self, simplisafe: SimpliSafe, system: SystemV3, sensor: SensorV3
) -> None:
"""Initialize."""
super().__init__(simplisafe, system, device=sensor)
self._device: SensorV3
@callback
def async_update_from_rest_api(self) -> None:
"""Update the entity with the provided REST API data."""
self._attr_native_value = self._device.temperature