-
Notifications
You must be signed in to change notification settings - Fork 30
/
binary_sensor.py
134 lines (107 loc) · 4.33 KB
/
binary_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
"""Support for binary (door/window/smoke/leak) sensors."""
from datetime import timedelta
import logging
import async_timeout
import voluptuous as vol
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.const import (
CONF_HOST,
CONF_TOKEN
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from pyit600.exceptions import IT600AuthenticationError, IT600ConnectionError
from pyit600.gateway import IT600Gateway
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_TOKEN): cv.string,
}
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Salus thermostats from a config entry."""
await async_setup_platform(hass, config_entry.data, async_add_entities)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the binary_sensor platform."""
gateway = IT600Gateway(host=config[CONF_HOST], euid=config[CONF_TOKEN])
try:
await gateway.connect()
except IT600ConnectionError as ce:
_LOGGER.error("Connection error: check if you have specified gateway's HOST correctly.")
return False
except IT600AuthenticationError as ae:
_LOGGER.error("Authentication error: check if you have specified gateway's TOKEN correctly.")
return False
async def async_update_data():
"""Fetch data from API endpoint.
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
async with async_timeout.timeout(10):
await gateway.poll_status()
return gateway.get_binary_sensor_devices()
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
# Name of the data. For logging purposes.
name="sensor",
update_method=async_update_data,
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=30),
)
# Fetch initial data so we have data when entities subscribe
await coordinator.async_refresh()
async_add_entities(SalusBinarySensor(coordinator, idx, gateway) for idx
in coordinator.data)
class SalusBinarySensor(BinarySensorEntity):
"""Representation of a binary sensor."""
def __init__(self, coordinator, idx, gateway):
"""Initialize the sensor."""
self._coordinator = coordinator
self._idx = idx
self._gateway = gateway
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.data.get(self._idx).available
@property
def device_info(self):
"""Return the device info."""
return {
"name": self._coordinator.data.get(self._idx).name,
"identifiers": {("salus", self._coordinator.data.get(self._idx).unique_id)},
"manufacturer": self._coordinator.data.get(self._idx).manufacturer,
"model": self._coordinator.data.get(self._idx).model,
"sw_version": self._coordinator.data.get(self._idx).sw_version
}
@property
def unique_id(self):
"""Return the unique id."""
return self._coordinator.data.get(self._idx).unique_id
@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False
@property
def name(self):
"""Return the name of the sensor."""
return self._coordinator.data.get(self._idx).name
@property
def is_on(self):
"""Return the state of the sensor."""
return self._coordinator.data.get(self._idx).is_on
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._coordinator.data.get(self._idx).device_class