core/homeassistant/components/vera/sensor.py

111 lines
4.0 KiB
Python
Raw Normal View History

"""Support for Vera sensors."""
2021-03-18 13:43:52 +00:00
from __future__ import annotations
from datetime import timedelta
from typing import cast
2016-02-19 05:27:50 +00:00
import pyvera as veraApi
from homeassistant.components.sensor import (
DOMAIN as PLATFORM_DOMAIN,
ENTITY_ID_FORMAT,
SensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import convert
from . import VeraDevice
from .common import ControllerData, get_controller_data
2015-09-09 03:11:25 +00:00
SCAN_INTERVAL = timedelta(seconds=5)
2015-03-08 14:58:11 +00:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensor config entry."""
controller_data = get_controller_data(hass, entry)
async_add_entities(
2019-07-31 19:25:30 +00:00
[
VeraSensor(device, controller_data)
for device in controller_data.devices.get(PLATFORM_DOMAIN)
],
True,
2019-07-31 19:25:30 +00:00
)
2015-03-08 14:58:11 +00:00
class VeraSensor(VeraDevice[veraApi.VeraSensor], SensorEntity):
2016-03-08 15:46:34 +00:00
"""Representation of a Vera Sensor."""
2015-03-02 10:09:00 +00:00
def __init__(
self, vera_device: veraApi.VeraSensor, controller_data: ControllerData
) -> None:
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-03-15 09:17:09 +00:00
self.current_value = None
self._temperature_units = None
self.last_changed_time = None
VeraDevice.__init__(self, vera_device, controller_data)
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
2015-03-02 10:09:00 +00:00
@property
def state(self) -> str:
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
2015-03-02 10:09:00 +00:00
return self.current_value
@property
2021-03-18 13:43:52 +00:00
def unit_of_measurement(self) -> str | None:
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
2019-07-31 19:25:30 +00:00
if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
return self._temperature_units
if self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
return LIGHT_LUX
if self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
2019-07-31 19:25:30 +00:00
return "level"
if self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
return PERCENTAGE
if self.vera_device.category == veraApi.CATEGORY_POWER_METER:
2019-07-31 19:25:30 +00:00
return "watts"
def update(self) -> None:
2016-03-08 15:46:34 +00:00
"""Update the state."""
2021-02-08 14:25:54 +00:00
super().update()
if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
2016-08-05 05:37:30 +00:00
self.current_value = self.vera_device.temperature
2019-07-31 19:25:30 +00:00
vera_temp_units = self.vera_device.vera_controller.temperature_units
2019-07-31 19:25:30 +00:00
if vera_temp_units == "F":
self._temperature_units = TEMP_FAHRENHEIT
else:
2016-04-20 03:30:44 +00:00
self._temperature_units = TEMP_CELSIUS
elif self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
self.current_value = self.vera_device.light
elif self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
self.current_value = self.vera_device.light
elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
self.current_value = self.vera_device.humidity
elif self.vera_device.category == veraApi.CATEGORY_SCENE_CONTROLLER:
controller = cast(veraApi.VeraSceneController, self.vera_device)
value = controller.get_last_scene_id(True)
time = controller.get_last_scene_time(True)
if time == self.last_changed_time:
self.current_value = None
else:
self.current_value = value
self.last_changed_time = time
elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
power = convert(self.vera_device.power, float, 0)
self.current_value = int(round(power, 0))
elif self.vera_device.is_trippable:
tripped = self.vera_device.is_tripped
2019-07-31 19:25:30 +00:00
self.current_value = "Tripped" if tripped else "Not Tripped"
2015-03-02 10:09:00 +00:00
else:
2019-07-31 19:25:30 +00:00
self.current_value = "Unknown"