2017-08-28 15:57:45 +00:00
|
|
|
"""
|
|
|
|
Support for Rain Bird Irrigation system LNK WiFi Module.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/rainbird/
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import rainbird
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
from homeassistant.const import (CONF_PLATFORM, CONF_SWITCHES, CONF_ZONE,
|
|
|
|
CONF_FRIENDLY_NAME, CONF_TRIGGER_TIME,
|
|
|
|
CONF_SCAN_INTERVAL)
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
|
|
|
DEPENDENCIES = ['rainbird']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): rainbird.DOMAIN,
|
|
|
|
vol.Required(CONF_SWITCHES, default={}): vol.Schema({
|
|
|
|
cv.string: {
|
|
|
|
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Required(CONF_ZONE): cv.string,
|
|
|
|
vol.Required(CONF_TRIGGER_TIME): cv.string,
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL): cv.string,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
"""Set up Rain Bird switches over a Rain Bird controller."""
|
|
|
|
devices = []
|
|
|
|
rbdevice = hass.data.get("DATA_RAINBIRD")
|
2017-08-28 19:07:40 +00:00
|
|
|
for dev_id, switch in config.get(CONF_SWITCHES).items():
|
|
|
|
devices.append(RainBirdSwitch(rbdevice, switch, dev_id))
|
2017-08-28 15:57:45 +00:00
|
|
|
add_devices(devices)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class RainBirdSwitch(SwitchDevice):
|
|
|
|
"""Representation of a Rain Bird switch."""
|
|
|
|
|
2017-08-28 19:07:40 +00:00
|
|
|
def __init__(self, rb, dev, dev_id):
|
2017-08-28 15:57:45 +00:00
|
|
|
"""Initialize a Rain Bird Switch Device."""
|
|
|
|
self._rainbird = rb
|
2017-08-28 19:07:40 +00:00
|
|
|
self._devid = dev_id
|
2017-08-28 15:57:45 +00:00
|
|
|
self._zone = int(dev.get(CONF_ZONE))
|
|
|
|
self._name = dev.get(CONF_FRIENDLY_NAME, "Sprinker %s" % self._zone)
|
|
|
|
self._state = self.get_device_status()
|
|
|
|
self._duration = dev.get(CONF_TRIGGER_TIME)
|
|
|
|
self._attributes = {
|
|
|
|
"duration": self._duration,
|
2017-08-29 11:47:47 +00:00
|
|
|
"zone": self._zone
|
2017-08-28 15:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
return self._attributes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling state."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Get the name of the switch."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
|
|
|
self._state = True
|
|
|
|
self._rainbird.start_irrigation(self._zone, self._duration)
|
2017-08-29 11:45:18 +00:00
|
|
|
self._rainbird.setstate(self._zone)
|
2017-08-28 15:57:45 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
self._state = False
|
|
|
|
self._rainbird.stop_irrigation()
|
2017-08-29 11:45:18 +00:00
|
|
|
self._rainbird.setstate(0)
|
2017-08-28 15:57:45 +00:00
|
|
|
|
|
|
|
def get_device_status(self):
|
|
|
|
"""Get the status of the switch from Rain Bird Controller."""
|
|
|
|
return self._rainbird.state == self._zone
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update switch status."""
|
|
|
|
self._state = self.get_device_status()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
|
|
|
return self._state
|