-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
sensor.py
108 lines (86 loc) · 2.99 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Support for VersaSense sensor peripheral."""
from __future__ import annotations
import logging
from homeassistant.components.sensor import SensorEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
from .const import (
KEY_CONSUMER,
KEY_IDENTIFIER,
KEY_MEASUREMENT,
KEY_PARENT_MAC,
KEY_PARENT_NAME,
KEY_UNIT,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
if discovery_info is None:
return
consumer = hass.data[DOMAIN][KEY_CONSUMER]
sensor_list = []
for entity_info in discovery_info.values():
peripheral = hass.data[DOMAIN][entity_info[KEY_PARENT_MAC]][
entity_info[KEY_IDENTIFIER]
]
parent_name = entity_info[KEY_PARENT_NAME]
unit = entity_info[KEY_UNIT]
measurement = entity_info[KEY_MEASUREMENT]
sensor_list.append(
VSensor(peripheral, parent_name, unit, measurement, consumer)
)
async_add_entities(sensor_list)
class VSensor(SensorEntity):
"""Representation of a Sensor."""
def __init__(self, peripheral, parent_name, unit, measurement, consumer):
"""Initialize the sensor."""
self._state = None
self._available = True
self._name = f"{parent_name} {measurement}"
self._parent_mac = peripheral.parentMac
self._identifier = peripheral.identifier
self._unit = unit
self._measurement = measurement
self.consumer = consumer
@property
def unique_id(self):
"""Return the unique id of the sensor."""
return f"{self._parent_mac}/{self._identifier}/{self._measurement}"
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit
@property
def available(self):
"""Return if the sensor is available."""
return self._available
async def async_update(self) -> None:
"""Fetch new state data for the sensor."""
samples = await self.consumer.fetchPeripheralSample(
None, self._identifier, self._parent_mac
)
if samples is not None:
for sample in samples:
if sample.measurement == self._measurement:
self._available = True
self._state = sample.value
break
else:
_LOGGER.error("Sample unavailable")
self._available = False
self._state = None