-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
68 lines (50 loc) · 1.76 KB
/
switch.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
"""
Gardena smart sensor which registers a couple of sensors.
@ todo something with documentation
"""
import logging
from datetime import timedelta
from homeassistant.components.switch import SwitchDevice
from custom_components.gardena import (GARDENA_WATERING_COMPUTERS, GARDENA_LOGIN)
DEPENDENCIES = ['gardena']
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=5)
ICON_WATER_ON = 'mdi:water'
ICON_WATER_OFF = 'mdi:water-off'
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the gardena water computer as switches."""
dev = []
for watering_computer in hass.data[GARDENA_WATERING_COMPUTERS]:
dev.append(GardenaSmartSwitch(hass, watering_computer))
_LOGGER.debug("Adding gardena watering computers as switch")
add_entities(dev, True)
class GardenaSmartSwitch(SwitchDevice):
def __init__(self, hass, switch):
"""Initialize the Demo switch."""
self._gardena = hass.data[GARDENA_LOGIN]
self._switch = switch
self._name = switch.name
self._state = switch.get_valve_open()
def update(self):
_LOGGER.debug("Running Gardena update")
self._gardena.update_devices() # is a throttled update
self._state = self._switch.get_valve_open()
@property
def name(self):
return self._name
@property
def icon(self):
if self._state:
return ICON_WATER_ON
return ICON_WATER_OFF
@property
def is_on(self):
return self._state
def turn_on(self, **kwargs):
self._switch.start()
def turn_off(self, **kwargs):
self._switch.stop()
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._switch.get_info()