-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
150 lines (114 loc) · 4.19 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Support for Xiaomi Mi e-ink Clock, Temperature & Humidity BLE environmental sensor."""
"""Based on great work of @h4 - https://github.com/h4/LYWSD02-home-assistant"""
import logging
from datetime import timedelta
import voluptuous as vol
from lywsd02 import Lywsd02Client as Client
from homeassistant.util import Throttle
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_MAC,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_BATTERY,
)
_LOGGER = logging.getLogger(__name__)
CONF_UPDATE_INTERVAL = "update_interval"
DEFAULT_UPDATE_INTERVAL = 30
DEFAULT_NAME = "Mi eClock BT"
# Sensor types are defined like: Name, units
SENSOR_TYPES = {
"temperature": [DEVICE_CLASS_TEMPERATURE, "Temperature", "°C"],
"humidity": [DEVICE_CLASS_HUMIDITY, "Humidity", "%"],
"battery": [DEVICE_CLASS_BATTERY, "Battery", "%"],
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UPDATE_INTERVAL, default=DEFAULT_UPDATE_INTERVAL): cv.positive_int,
}
)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=DEFAULT_UPDATE_INTERVAL)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the MieClockBt sensor."""
global MIN_TIME_BETWEEN_UPDATES
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=config.get(CONF_UPDATE_INTERVAL))
mac = config.get(CONF_MAC)
# Configure the client.
client = Client(mac)
poller = MieClockData(client)
devs = []
for parameter in config[CONF_MONITORED_CONDITIONS]:
device = SENSOR_TYPES[parameter][0]
name = SENSOR_TYPES[parameter][1]
unit = SENSOR_TYPES[parameter][2]
prefix = config.get(CONF_NAME)
if prefix:
name = f"{prefix} {name}"
devs.append(
MieClockBtSensor(poller, parameter, device, name, unit)
)
add_entities(devs)
class MieClockBtSensor(Entity):
"""Implementing the MieClockBt sensor."""
def __init__(self, poller, parameter, device, name, unit):
"""Initialize the sensor."""
self.parameter = parameter
self._device = device
self._unit = unit
self._name = name
self._state = None
self._poller = poller
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return self._unit
@property
def device_class(self):
"""Device class of this entity."""
return self._device
def update(self):
"""Update the sensor."""
# Send update "signal" to the component
self._poller.update_data()
# Get new data (if any)
updated = self._poller.data
# Check the data and update the value.
self._state = updated.get(self.parameter)
class MieClockData:
"""This class handles communication and stores the data."""
def __init__(self, client: Client):
"""Initialize the class."""
self.client = client
self.data = {}
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update_data(self):
import decimal
from decimal import Decimal
decimal.getcontext().rounding = decimal.ROUND_DOWN
"""Update data."""
# This is where the main logic to update platform data goes.
try:
self.data = {
'temperature': Decimal(self.client.temperature).quantize(Decimal("1.0")),
'humidity': self.client.humidity,
'battery': self.client.battery,
}
except Exception as error: # pylint: disable=broad-except
_LOGGER.error("Could not update data - %s", error)