2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Velbus sensors."""
|
2021-09-13 06:22:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
STATE_CLASS_MEASUREMENT,
|
|
|
|
STATE_CLASS_TOTAL_INCREASING,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_ENERGY,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
)
|
2020-01-26 17:48:20 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
from . import VelbusEntity
|
2019-12-09 08:36:42 +00:00
|
|
|
from .const import DOMAIN
|
2018-08-27 04:06:46 +00:00
|
|
|
|
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
2021-09-13 06:22:46 +00:00
|
|
|
"""Set up Velbus switch based on config_entry."""
|
|
|
|
await hass.data[DOMAIN][entry.entry_id]["tsk"]
|
2019-07-31 19:25:30 +00:00
|
|
|
cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
|
2019-07-29 07:21:26 +00:00
|
|
|
entities = []
|
2021-09-13 06:22:46 +00:00
|
|
|
for channel in cntrl.get_all("sensor"):
|
|
|
|
entities.append(VelbusSensor(channel))
|
|
|
|
if channel.is_counter_channel():
|
|
|
|
entities.append(VelbusSensor(channel, True))
|
2019-07-29 07:21:26 +00:00
|
|
|
async_add_entities(entities)
|
2018-08-27 04:06:46 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class VelbusSensor(VelbusEntity, SensorEntity):
|
2018-09-18 13:51:22 +00:00
|
|
|
"""Representation of a sensor."""
|
2018-08-27 04:06:46 +00:00
|
|
|
|
2021-09-13 06:22:46 +00:00
|
|
|
def __init__(self, channel, counter=False):
|
2020-01-26 17:48:20 +00:00
|
|
|
"""Initialize a sensor Velbus entity."""
|
2021-09-13 06:22:46 +00:00
|
|
|
super().__init__(channel)
|
2020-01-26 17:48:20 +00:00
|
|
|
self._is_counter = counter
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for counter sensors."""
|
|
|
|
unique_id = super().unique_id
|
|
|
|
if self._is_counter:
|
|
|
|
unique_id = f"{unique_id}-counter"
|
|
|
|
return unique_id
|
|
|
|
|
2021-09-13 06:22:46 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name for the sensor."""
|
|
|
|
name = super().name
|
|
|
|
if self._is_counter:
|
|
|
|
name = f"{name}-counter"
|
|
|
|
return name
|
|
|
|
|
2018-08-27 04:06:46 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
2021-09-13 06:22:46 +00:00
|
|
|
if self._is_counter:
|
|
|
|
return DEVICE_CLASS_ENERGY
|
|
|
|
if self._channel.is_counter_channel():
|
|
|
|
return DEVICE_CLASS_POWER
|
|
|
|
if self._channel.is_temperature():
|
|
|
|
return DEVICE_CLASS_TEMPERATURE
|
|
|
|
return None
|
2018-08-27 04:06:46 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self):
|
2018-08-27 04:06:46 +00:00
|
|
|
"""Return the state of the sensor."""
|
2020-01-26 17:48:20 +00:00
|
|
|
if self._is_counter:
|
2021-09-13 06:22:46 +00:00
|
|
|
return self._channel.get_counter_state()
|
|
|
|
return self._channel.get_state()
|
2018-08-27 04:06:46 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2018-08-27 04:06:46 +00:00
|
|
|
"""Return the unit this state is expressed in."""
|
2020-01-26 17:48:20 +00:00
|
|
|
if self._is_counter:
|
2021-09-13 06:22:46 +00:00
|
|
|
return self._channel.get_counter_unit()
|
|
|
|
return self._channel.get_unit()
|
2020-01-26 17:48:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend."""
|
|
|
|
if self._is_counter:
|
|
|
|
return "mdi:counter"
|
|
|
|
return None
|
2021-09-13 06:22:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the state class of this device."""
|
|
|
|
if self._is_counter:
|
|
|
|
return STATE_CLASS_TOTAL_INCREASING
|
|
|
|
return STATE_CLASS_MEASUREMENT
|