-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
/
__init__.py
147 lines (113 loc) · 4.47 KB
/
__init__.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
"""Support for Hydrawise cloud."""
from datetime import timedelta
import logging
from requests.exceptions import ConnectTimeout, HTTPError
import voluptuous as vol
from homeassistant.const import (
ATTR_ATTRIBUTION, CONF_ACCESS_TOKEN, CONF_SCAN_INTERVAL)
import homeassistant.helpers.config_validation as cv
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, dispatcher_send)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_time_interval
REQUIREMENTS = ['hydrawiser==0.1.1']
_LOGGER = logging.getLogger(__name__)
ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60]
ATTRIBUTION = "Data provided by hydrawise.com"
CONF_WATERING_TIME = 'watering_minutes'
NOTIFICATION_ID = 'hydrawise_notification'
NOTIFICATION_TITLE = 'Hydrawise Setup'
DATA_HYDRAWISE = 'hydrawise'
DOMAIN = 'hydrawise'
DEFAULT_WATERING_TIME = 15
DEVICE_MAP_INDEX = ['KEY_INDEX', 'ICON_INDEX', 'DEVICE_CLASS_INDEX',
'UNIT_OF_MEASURE_INDEX']
DEVICE_MAP = {
'auto_watering': ['Automatic Watering', 'mdi:autorenew', '', ''],
'is_watering': ['Watering', '', 'moisture', ''],
'manual_watering': ['Manual Watering', 'mdi:water-pump', '', ''],
'next_cycle': ['Next Cycle', 'mdi:calendar-clock', '', ''],
'status': ['Status', '', 'connectivity', ''],
'watering_time': ['Watering Time', 'mdi:water-pump', '', 'min'],
'rain_sensor': ['Rain Sensor', '', 'moisture', '']
}
BINARY_SENSORS = ['is_watering', 'status', 'rain_sensor']
SENSORS = ['next_cycle', 'watering_time']
SWITCHES = ['auto_watering', 'manual_watering']
SCAN_INTERVAL = timedelta(seconds=30)
SIGNAL_UPDATE_HYDRAWISE = "hydrawise_update"
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL):
cv.time_period,
}),
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up the Hunter Hydrawise component."""
conf = config[DOMAIN]
access_token = conf[CONF_ACCESS_TOKEN]
scan_interval = conf.get(CONF_SCAN_INTERVAL)
try:
from hydrawiser.core import Hydrawiser
hydrawise = Hydrawiser(user_token=access_token)
hass.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise)
except (ConnectTimeout, HTTPError) as ex:
_LOGGER.error(
"Unable to connect to Hydrawise cloud service: %s", str(ex))
hass.components.persistent_notification.create(
'Error: {}<br />'
'You will need to restart hass after fixing.'
''.format(ex),
title=NOTIFICATION_TITLE,
notification_id=NOTIFICATION_ID)
return False
def hub_refresh(event_time):
"""Call Hydrawise hub to refresh information."""
_LOGGER.debug("Updating Hydrawise Hub component")
hass.data[DATA_HYDRAWISE].data.update_controller_info()
dispatcher_send(hass, SIGNAL_UPDATE_HYDRAWISE)
# Call the Hydrawise API to refresh updates
track_time_interval(hass, hub_refresh, scan_interval)
return True
class HydrawiseHub:
"""Representation of a base Hydrawise device."""
def __init__(self, data):
"""Initialize the entity."""
self.data = data
class HydrawiseEntity(Entity):
"""Entity class for Hydrawise devices."""
def __init__(self, data, sensor_type):
"""Initialize the Hydrawise entity."""
self.data = data
self._sensor_type = sensor_type
self._name = "{0} {1}".format(
self.data['name'],
DEVICE_MAP[self._sensor_type][
DEVICE_MAP_INDEX.index('KEY_INDEX')])
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
async def async_added_to_hass(self):
"""Register callbacks."""
async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_HYDRAWISE, self._update_callback)
@callback
def _update_callback(self):
"""Call update method."""
self.async_schedule_update_ha_state(True)
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return DEVICE_MAP[self._sensor_type][
DEVICE_MAP_INDEX.index('UNIT_OF_MEASURE_INDEX')]
@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
ATTR_ATTRIBUTION: ATTRIBUTION,
'identifier': self.data.get('relay'),
}