Created
April 13, 2019 13:03
-
-
Save gieljnssns/8cb1946eb11889ef4f23d340c1b13ab8 to your computer and use it in GitHub Desktop.
rainbird2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Support for Rain Bird Irrigation system LNK WiFi Module.""" | |
import logging | |
import voluptuous as vol | |
import homeassistant.helpers.config_validation as cv | |
from homeassistant.const import (CONF_HOST, CONF_PASSWORD) | |
_LOGGER = logging.getLogger(__name__) | |
DATA_RAINBIRD2 = 'rainbird2' | |
DOMAIN = 'rainbird2' | |
CONFIG_SCHEMA = vol.Schema({ | |
DOMAIN: vol.Schema({ | |
vol.Required(CONF_HOST): cv.string, | |
vol.Required(CONF_PASSWORD): cv.string, | |
}), | |
}, extra=vol.ALLOW_EXTRA) | |
def setup(hass, config): | |
"""Set up the Rain Bird component.""" | |
conf = config[DOMAIN] | |
server = conf.get(CONF_HOST) | |
password = conf.get(CONF_PASSWORD) | |
from pyrainbird import RainbirdController | |
controller = RainbirdController() | |
controller.setConfig(server, password) | |
_LOGGER.debug("Rain Bird Controller set to: %s", server) | |
initial_status = controller.currentIrrigation() | |
if initial_status == -1: | |
_LOGGER.error("Error getting state. Possible configuration issues") | |
return False | |
hass.data[DATA_RAINBIRD2] = controller | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"domain": "rainbird2", | |
"name": "Rainbird2", | |
"documentation": "https://www.home-assistant.io/components/rainbird", | |
"requirements": [ | |
"pyrainbird==0.1.6" | |
], | |
"dependencies": [], | |
"codeowners": [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Support for Rain Bird Irrigation system LNK WiFi Module.""" | |
import logging | |
import voluptuous as vol | |
from homeassistant.components.sensor import PLATFORM_SCHEMA | |
from homeassistant.const import CONF_MONITORED_CONDITIONS | |
import homeassistant.helpers.config_validation as cv | |
from homeassistant.helpers.entity import Entity | |
from . import DATA_RAINBIRD2 | |
_LOGGER = logging.getLogger(__name__) | |
# sensor_type [ description, unit, icon ] | |
SENSOR_TYPES = { | |
'rainsensor': ['Rainsensor', None, 'mdi:water'] | |
} | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): | |
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), | |
}) | |
def setup_platform(hass, config, add_entities, discovery_info=None): | |
"""Set up a Rain Bird sensor.""" | |
controller = hass.data[DATA_RAINBIRD2] | |
sensors = [] | |
for sensor_type in config.get(CONF_MONITORED_CONDITIONS): | |
sensors.append( | |
RainBirdSensor(controller, sensor_type)) | |
add_entities(sensors, True) | |
class RainBirdSensor(Entity): | |
"""A sensor implementation for Rain Bird device.""" | |
def __init__(self, controller, sensor_type): | |
"""Initialize the Rain Bird sensor.""" | |
self._sensor_type = sensor_type | |
self._controller = controller | |
self._name = SENSOR_TYPES[self._sensor_type][0] | |
self._icon = SENSOR_TYPES[self._sensor_type][2] | |
self._unit_of_measurement = SENSOR_TYPES[self._sensor_type][1] | |
self._state = None | |
@property | |
def state(self): | |
"""Return the state of the sensor.""" | |
return self._state | |
def update(self): | |
"""Get the latest data and updates the states.""" | |
_LOGGER.debug("Updating sensor: %s", self._name) | |
if self._sensor_type == 'rainsensor': | |
self._state = self._controller.currentRainSensorState() | |
@property | |
def name(self): | |
"""Return the name of this camera.""" | |
return self._name | |
@property | |
def unit_of_measurement(self): | |
"""Return the units of measurement.""" | |
return self._unit_of_measurement | |
@property | |
def icon(self): | |
"""Return icon.""" | |
return self._icon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Support for Rain Bird Irrigation system LNK WiFi Module.""" | |
import logging | |
import voluptuous as vol | |
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice | |
from homeassistant.const import ( | |
CONF_FRIENDLY_NAME, CONF_SCAN_INTERVAL, CONF_SWITCHES, CONF_TRIGGER_TIME, | |
CONF_ZONE) | |
from homeassistant.helpers import config_validation as cv | |
from . import DATA_RAINBIRD2 | |
DOMAIN = 'rainbird2' | |
_LOGGER = logging.getLogger(__name__) | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
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_entities, discovery_info=None): | |
"""Set up Rain Bird switches over a Rain Bird controller.""" | |
controller = hass.data[DATA_RAINBIRD2] | |
devices = [] | |
for dev_id, switch in config.get(CONF_SWITCHES).items(): | |
devices.append(RainBirdSwitch(controller, switch, dev_id)) | |
add_entities(devices, True) | |
class RainBirdSwitch(SwitchDevice): | |
"""Representation of a Rain Bird switch.""" | |
def __init__(self, rb, dev, dev_id): | |
"""Initialize a Rain Bird Switch Device.""" | |
self._rainbird = rb | |
self._devid = dev_id | |
self._zone = int(dev.get(CONF_ZONE)) | |
self._name = dev.get(CONF_FRIENDLY_NAME, | |
"Sprinkler {}".format(self._zone)) | |
self._state = None | |
self._duration = dev.get(CONF_TRIGGER_TIME) | |
self._attributes = { | |
"duration": self._duration, | |
"zone": self._zone | |
} | |
@property | |
def device_state_attributes(self): | |
"""Return state attributes.""" | |
return self._attributes | |
@property | |
def name(self): | |
"""Get the name of the switch.""" | |
return self._name | |
def turn_on(self, **kwargs): | |
"""Turn the switch on.""" | |
self._rainbird.startIrrigation(int(self._zone), int(self._duration)) | |
def turn_off(self, **kwargs): | |
"""Turn the switch off.""" | |
self._rainbird.stopIrrigation() | |
def get_device_status(self): | |
"""Get the status of the switch from Rain Bird Controller.""" | |
return self._rainbird.currentIrrigation() == 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment