commit
e9f36a7e45
|
@ -9,8 +9,6 @@ import logging
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.const import (CONF_PLATFORM, CONF_SWITCHES, CONF_ZONE,
|
||||
CONF_FRIENDLY_NAME, CONF_TRIGGER_TIME,
|
||||
|
@ -18,7 +16,7 @@ from homeassistant.const import (CONF_PLATFORM, CONF_SWITCHES, CONF_ZONE,
|
|||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
|
||||
REQUIREMENTS = ['pyrainbird==0.0.9']
|
||||
REQUIREMENTS = ['pyrainbird==0.1.0']
|
||||
|
||||
DOMAIN = 'rainbird'
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -42,29 +40,23 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
"""Set up Rain Bird switches over a Rain Bird controller."""
|
||||
server = config.get(CONF_HOST)
|
||||
password = config.get(CONF_PASSWORD)
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
from pyrainbird import RainbirdController
|
||||
|
||||
controller = RainbirdController(_LOGGER)
|
||||
controller.setConfig(server, password)
|
||||
_LOGGER.info("Rain Bird Controller set to " + str(server))
|
||||
|
||||
rbdevice = RainbirdDevice(hass, controller)
|
||||
initialstatus = rbdevice.update()
|
||||
_LOGGER.debug("Rain Bird Controller set to " + str(server))
|
||||
|
||||
initialstatus = controller.currentIrrigation()
|
||||
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)
|
||||
_LOGGER.debug("Initialized Rain Bird Controller")
|
||||
|
||||
devices = []
|
||||
for dev_id, switch in config.get(CONF_SWITCHES).items():
|
||||
devices.append(RainBirdSwitch(rbdevice, switch, dev_id))
|
||||
devices.append(RainBirdSwitch(controller, switch, dev_id))
|
||||
add_devices(devices)
|
||||
return True
|
||||
|
||||
|
@ -103,18 +95,16 @@ class RainBirdSwitch(SwitchDevice):
|
|||
def turn_on(self, **kwargs):
|
||||
"""Turn the switch on."""
|
||||
self._state = True
|
||||
self._rainbird.start_irrigation(self._zone, self._duration)
|
||||
self._rainbird.setstate(self._zone)
|
||||
self._rainbird.startIrrigation(int(self._zone), int(self._duration))
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the switch off."""
|
||||
self._state = False
|
||||
self._rainbird.stop_irrigation()
|
||||
self._rainbird.setstate(0)
|
||||
self._rainbird.stopIrrigation()
|
||||
|
||||
def get_device_status(self):
|
||||
"""Get the status of the switch from Rain Bird Controller."""
|
||||
return self._rainbird.state == self._zone
|
||||
return self._rainbird.currentIrrigation() == self._zone
|
||||
|
||||
def update(self):
|
||||
"""Update switch status."""
|
||||
|
@ -124,101 +114,3 @@ class RainBirdSwitch(SwitchDevice):
|
|||
def is_on(self):
|
||||
"""Return true if switch is on."""
|
||||
return self._state
|
||||
|
||||
|
||||
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
|
||||
|
||||
def setstate(self, state):
|
||||
"""Force set the current state value."""
|
||||
self._state = state
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the entity."""
|
||||
return self._state
|
||||
|
||||
def start_irrigation(self, station_id, duration):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
_LOGGER.info("Requesting irrigation for " +
|
||||
str(station_id) + " duration " + str(duration))
|
||||
result = self.controller.startIrrigation(
|
||||
int(station_id), int(duration))
|
||||
if result == 1:
|
||||
_LOGGER.info("Irrigation started on " + str(station_id) +
|
||||
" for " + str(duration))
|
||||
elif result == 0:
|
||||
_LOGGER.error("Error sending request")
|
||||
else:
|
||||
_LOGGER.error("Request was not acknowledged!")
|
||||
|
||||
def stop_irrigation(self):
|
||||
"""Stop the irrigation (if one is running)."""
|
||||
_LOGGER.info("Stop request irrigation")
|
||||
result = self.controller.stopIrrigation()
|
||||
if result == 1:
|
||||
_LOGGER.info("Stopped irrigation")
|
||||
elif result == 0:
|
||||
_LOGGER.error("Error sending request")
|
||||
else:
|
||||
_LOGGER.error("Request was not acknowledged!")
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Get current active station.
|
||||
|
||||
@return: integer which station is active
|
||||
"""
|
||||
_LOGGER.info("Request irrigation state")
|
||||
result = self.controller.currentIrrigation()
|
||||
if result < 0:
|
||||
_LOGGER.error("Error sending request")
|
||||
return -1
|
||||
self._state = result
|
||||
|
|
|
@ -709,7 +709,7 @@ pyowm==2.7.1
|
|||
pyqwikswitch==0.4
|
||||
|
||||
# homeassistant.components.switch.rainbird
|
||||
pyrainbird==0.0.9
|
||||
pyrainbird==0.1.0
|
||||
|
||||
# homeassistant.components.climate.sensibo
|
||||
pysensibo==1.0.1
|
||||
|
|
Loading…
Reference in New Issue