2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Vera sensors."""
|
2017-04-25 07:17:25 +00:00
|
|
|
from datetime import timedelta
|
2020-09-16 21:23:50 +00:00
|
|
|
from typing import Callable, List, Optional, cast
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2019-10-12 19:55:40 +00:00
|
|
|
import pyvera as veraApi
|
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as PLATFORM_DOMAIN, ENTITY_ID_FORMAT
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-23 18:48:01 +00:00
|
|
|
from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
2020-04-03 07:49:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-04-18 10:01:23 +00:00
|
|
|
from homeassistant.util import convert
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
from . import VeraDevice
|
2020-09-15 03:06:52 +00:00
|
|
|
from .common import ControllerData, get_controller_data
|
2015-09-09 03:11:25 +00:00
|
|
|
|
2017-04-25 07:17:25 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2020-04-03 07:49:50 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
2020-09-15 03:06:52 +00:00
|
|
|
controller_data = get_controller_data(hass, entry)
|
2020-04-03 07:49:50 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2020-09-15 03:06:52 +00:00
|
|
|
VeraSensor(device, controller_data)
|
2020-04-03 07:49:50 +00:00
|
|
|
for device in controller_data.devices.get(PLATFORM_DOMAIN)
|
2021-02-05 12:20:15 +00:00
|
|
|
],
|
|
|
|
True,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-03-07 01:59:13 +00:00
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
class VeraSensor(VeraDevice[veraApi.VeraSensor], Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Vera Sensor."""
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def __init__(
|
|
|
|
self, vera_device: veraApi.VeraSensor, controller_data: ControllerData
|
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self.current_value = None
|
2015-03-29 13:51:03 +00:00
|
|
|
self._temperature_units = None
|
2017-04-25 07:17:25 +00:00
|
|
|
self.last_changed_time = None
|
2020-09-15 03:06:52 +00:00
|
|
|
VeraDevice.__init__(self, vera_device, controller_data)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2015-03-02 10:09:00 +00:00
|
|
|
|
|
|
|
@property
|
2020-09-16 21:23:50 +00:00
|
|
|
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
|
|
|
|
|
2015-03-29 13:51:03 +00:00
|
|
|
@property
|
2020-09-16 21:23:50 +00:00
|
|
|
def unit_of_measurement(self) -> Optional[str]:
|
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
|
|
|
|
2017-09-29 21:34:14 +00:00
|
|
|
if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
|
2016-01-15 09:21:48 +00:00
|
|
|
return self._temperature_units
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
|
2020-09-23 18:48:01 +00:00
|
|
|
return LIGHT_LUX
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "level"
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.vera_device.category == veraApi.CATEGORY_POWER_METER:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "watts"
|
2015-03-29 13:51:03 +00:00
|
|
|
|
2020-09-16 21:23:50 +00:00
|
|
|
def update(self) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Update the state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-09-29 21:34:14 +00:00
|
|
|
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
|
2015-03-29 13:51:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if vera_temp_units == "F":
|
2015-03-29 13:51:03 +00:00
|
|
|
self._temperature_units = TEMP_FAHRENHEIT
|
|
|
|
else:
|
2016-04-20 03:30:44 +00:00
|
|
|
self._temperature_units = TEMP_CELSIUS
|
2015-03-29 13:51:03 +00:00
|
|
|
|
2017-09-29 21:34:14 +00:00
|
|
|
elif self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
|
2016-01-15 21:30:58 +00:00
|
|
|
self.current_value = self.vera_device.light
|
2017-10-12 17:51:25 +00:00
|
|
|
elif self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
|
|
|
|
self.current_value = self.vera_device.light
|
2017-09-29 21:34:14 +00:00
|
|
|
elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
|
2016-01-15 21:30:58 +00:00
|
|
|
self.current_value = self.vera_device.humidity
|
2017-09-29 21:34:14 +00:00
|
|
|
elif self.vera_device.category == veraApi.CATEGORY_SCENE_CONTROLLER:
|
2020-09-16 21:23:50 +00:00
|
|
|
controller = cast(veraApi.VeraSceneController, self.vera_device)
|
|
|
|
value = controller.get_last_scene_id(True)
|
|
|
|
time = controller.get_last_scene_time(True)
|
2017-04-25 07:17:25 +00:00
|
|
|
if time == self.last_changed_time:
|
|
|
|
self.current_value = None
|
|
|
|
else:
|
|
|
|
self.current_value = value
|
|
|
|
self.last_changed_time = time
|
2017-09-29 21:34:14 +00:00
|
|
|
elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
|
2017-04-18 10:01:23 +00:00
|
|
|
power = convert(self.vera_device.power, float, 0)
|
|
|
|
self.current_value = int(round(power, 0))
|
2017-09-29 21:34:14 +00:00
|
|
|
elif self.vera_device.is_trippable:
|
2016-01-15 21:30:58 +00:00
|
|
|
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"
|