Changed component to use entity and switch
Changed component to use entity and switch.pull/9334/head
parent
21cca21124
commit
0eee544d17
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
Support for Rainbird Irrigation system WiFi LNK Module.
|
||||
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/
|
||||
|
@ -7,15 +7,16 @@ https://home-assistant.io/components/rainbird/
|
|||
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers as helpers
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD)
|
||||
|
||||
REQUIREMENTS = ['pyrainbird==0.0.7']
|
||||
|
||||
DOMAIN = 'rainbird'
|
||||
STATE_VAR = 'rainbird.activestation'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -28,29 +29,95 @@ CONFIG_SCHEMA = vol.Schema({
|
|||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Rainbird component."""
|
||||
"""Set up the Rain Bird component."""
|
||||
server = config[DOMAIN].get(CONF_HOST)
|
||||
password = config[DOMAIN].get(CONF_PASSWORD)
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
# RainbirdSetup
|
||||
from pyrainbird import RainbirdController
|
||||
|
||||
controller = RainbirdController(_LOGGER)
|
||||
controller.setConfig(server, password)
|
||||
_LOGGER.info("Rainbird Controller set to " + str(server))
|
||||
_LOGGER.info("Rain Bird Controller set to " + str(server))
|
||||
|
||||
def startirrigation(call):
|
||||
rbdevice = RainbirdDevice(hass, controller)
|
||||
hass.data["DATA_RAINBIRD"] = rbdevice
|
||||
|
||||
initialstatus = rbdevice.update()
|
||||
if initialstatus == -1:
|
||||
_LOGGER.error("Error getting state. Possible configuration issues")
|
||||
raise PlatformNotReady
|
||||
else:
|
||||
_LOGGER.info("Initialized Rain Bird Controller")
|
||||
|
||||
entities = []
|
||||
entities.append(rbdevice)
|
||||
component.add_entities(entities)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class RainbirdDevice(Entity):
|
||||
"""Rain Bird Device."""
|
||||
|
||||
_state = -1
|
||||
|
||||
def __init__(self, hass, controller):
|
||||
"""Initialize the device."""
|
||||
self.hass = hass
|
||||
self.controller = controller
|
||||
self._name = "Rainbird_Controller"
|
||||
self._stations = {}
|
||||
|
||||
# For automation purposes add 2 services
|
||||
def start_irrigation_call(call):
|
||||
"""Start irrigation from service call."""
|
||||
station_id = call.data.get("station_id")
|
||||
duration = call.data.get("duration")
|
||||
if station_id and duration:
|
||||
self.start_irrigation(station_id, duration)
|
||||
else:
|
||||
_LOGGER.warning("Error in start_irrigation call. \
|
||||
station_id and duration need to be set")
|
||||
|
||||
def stop_irrigation_call(call):
|
||||
"""Start irrigation from service call."""
|
||||
self.stop_irrigation()
|
||||
|
||||
hass.services.register(DOMAIN, 'start_irrigation',
|
||||
start_irrigation_call)
|
||||
hass.services.register(DOMAIN, 'stop_irrigation',
|
||||
stop_irrigation_call)
|
||||
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return True
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Get the name of the device."""
|
||||
return self._name
|
||||
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._state != -1
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
def start_irrigation(self, station_id, duration):
|
||||
"""
|
||||
Start Irrigation command towards Rainbird WiFi LNK stick.
|
||||
Start Irrigation command towards Rain Bird LNK WiFi stick.
|
||||
|
||||
@param call: should be a home assistant call object with data
|
||||
station for Zone to sprinkle and duration for the time
|
||||
"""
|
||||
station_id = call.data.get('station')
|
||||
duration = call.data.get('duration')
|
||||
_LOGGER.info("Requesting irrigation for " +
|
||||
str(station_id) + " duration " + str(duration))
|
||||
result = controller.startIrrigation(station_id, duration)
|
||||
result = self.controller.startIrrigation(
|
||||
int(station_id), int(duration))
|
||||
if result == 1:
|
||||
_LOGGER.info("Irrigation started on " + str(station_id) +
|
||||
" for " + str(duration))
|
||||
|
@ -59,10 +126,10 @@ def setup(hass, config):
|
|||
else:
|
||||
_LOGGER.error("Request was not acknowledged!")
|
||||
|
||||
def stopirrigation():
|
||||
def stop_irrigation(self):
|
||||
"""Stop the irrigation (if one is running)."""
|
||||
_LOGGER.info("Stop request irrigation")
|
||||
result = controller.stopIrrigation()
|
||||
result = self.controller.stopIrrigation()
|
||||
if result == 1:
|
||||
_LOGGER.info("Stopped irrigation")
|
||||
elif result == 0:
|
||||
|
@ -70,30 +137,15 @@ def setup(hass, config):
|
|||
else:
|
||||
_LOGGER.error("Request was not acknowledged!")
|
||||
|
||||
def getirrigation():
|
||||
def update(self):
|
||||
"""
|
||||
Get current active station.
|
||||
|
||||
@return: integer which station is active
|
||||
"""
|
||||
_LOGGER.info("Request irrigation state")
|
||||
result = controller.currentIrrigation()
|
||||
result = self.controller.currentIrrigation()
|
||||
if result < 0:
|
||||
_LOGGER.error("Error sending request")
|
||||
return -1
|
||||
|
||||
return result
|
||||
initialstatus = getirrigation()
|
||||
hass.states.set(STATE_VAR, initialstatus)
|
||||
|
||||
hass.services.register(DOMAIN, 'start_irrigation', startirrigation)
|
||||
hass.services.register(DOMAIN, 'stop_irrigation', stopirrigation)
|
||||
|
||||
helpers.event.track_time_change(
|
||||
hass, lambda _: hass.states.set(STATE_VAR, getirrigation()),
|
||||
year=None, month=None, day=None,
|
||||
hour=None, minute=None, second=[00, 30]
|
||||
)
|
||||
_LOGGER.info("Initialized Rainbird Controller")
|
||||
|
||||
return True
|
||||
self._state = result
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
"""
|
||||
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")
|
||||
for key, switch in config.get(CONF_SWITCHES).items():
|
||||
devices.append(RainBirdSwitch(rbdevice, switch))
|
||||
add_devices(devices)
|
||||
return True
|
||||
|
||||
|
||||
class RainBirdSwitch(SwitchDevice):
|
||||
"""Representation of a Rain Bird switch."""
|
||||
|
||||
def __init__(self, rb, dev):
|
||||
"""Initialize a Rain Bird Switch Device."""
|
||||
self._rainbird = rb
|
||||
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,
|
||||
}
|
||||
|
||||
@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)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the switch off."""
|
||||
self._state = False
|
||||
self._rainbird.stop_irrigation()
|
||||
|
||||
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
|
Loading…
Reference in New Issue