-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
sensor.py
53 lines (41 loc) · 1.58 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Support for KNX/IP sensors."""
from xknx.devices import Sensor as XknxSensor
from homeassistant.components.sensor import DEVICE_CLASSES
from homeassistant.helpers.entity import Entity
from .const import DOMAIN
from .knx_entity import KnxEntity
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up sensor(s) for KNX platform."""
entities = []
for device in hass.data[DOMAIN].xknx.devices:
if isinstance(device, XknxSensor):
entities.append(KNXSensor(device))
async_add_entities(entities)
class KNXSensor(KnxEntity, Entity):
"""Representation of a KNX sensor."""
def __init__(self, device: XknxSensor):
"""Initialize of a KNX sensor."""
super().__init__(device)
@property
def state(self):
"""Return the state of the sensor."""
return self._device.resolve_state()
@property
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
return self._device.unit_of_measurement()
@property
def device_class(self):
"""Return the device class of the sensor."""
device_class = self._device.ha_device_class()
if device_class in DEVICE_CLASSES:
return device_class
return None
@property
def force_update(self) -> bool:
"""
Return True if state updates should be forced.
If True, a state change will be triggered anytime the state property is
updated, not just when the value changes.
"""
return self._device.always_callback