-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsensor.py
106 lines (84 loc) · 2.95 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
"""Support for Daikin AC sensors."""
from homeassistant.const import (
CONF_TYPE,
CONF_UNIT_OF_MEASUREMENT,
UnitOfTemperature
)
from homeassistant.components.sensor import (
SensorDeviceClass
)
from homeassistant.helpers.entity import Entity
from . import DOMAIN
from .const import (
SENSOR_TYPE_TEMPERATURE,
CONTROLLERS,
)
from pymadoka import Controller
from pymadoka.feature import ConnectionException, ConnectionStatus
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Old way of setting up the Daikin sensors.
Can only be called when a user accidentally mentions the platform in their
config. But even in that case it would have been ignored.
"""
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Daikin climate based on config_entry."""
ent = []
for controller in hass.data[DOMAIN][CONTROLLERS].values():
ent.append(MadokaSensor(controller))
async_add_entities(ent)
class MadokaSensor(Entity):
"""Representation of a Sensor."""
def __init__(self, controller: Controller) -> None:
"""Initialize the sensor."""
self.controller = controller
self._sensor = {
CONF_TYPE: SENSOR_TYPE_TEMPERATURE,
CONF_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS,
}
@property
def available(self):
"""Return the availability."""
return self.controller.connection.connection_status is ConnectionStatus.CONNECTED
@property
def unique_id(self):
"""Return a unique ID."""
return self.controller.connection.address
@property
def name(self):
"""Return the name of the thermostat, if any."""
return self.controller.connection.name if self.controller.connection.name is not None else self.controller.connection.address
@property
def state(self):
"""Return the internal state of the sensor."""
if self.controller.temperatures.status is None:
return None
return self.controller.temperatures.status.indoor
@property
def device_class(self):
"""Return the class of this device."""
return SensorDeviceClass.TEMPERATURE
@property
def icon(self):
"""Return the icon of this device."""
return None
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return UnitOfTemperature.CELSIUS
async def async_update(self):
"""Retrieve latest state."""
try:
await self.controller.temperatures.query()
except ConnectionAbortedError:
pass
except ConnectionException:
pass
@property
async def async_device_info(self):
"""Return a device description for device registry."""
try:
return await self.controller.read_info()
except ConnectionAbortedError:
pass
except ConnectionException:
pass