diff --git a/.coveragerc b/.coveragerc index 4751ddce219..83f11983806 100644 --- a/.coveragerc +++ b/.coveragerc @@ -477,6 +477,7 @@ omit = homeassistant/components/notify/yessssms.py homeassistant/components/nuimo_controller.py homeassistant/components/prometheus.py + homeassistant/components/rainbird.py homeassistant/components/remember_the_milk/__init__.py homeassistant/components/remote/harmony.py homeassistant/components/remote/itach.py @@ -571,6 +572,7 @@ omit = homeassistant/components/sensor/pyload.py homeassistant/components/sensor/qnap.py homeassistant/components/sensor/radarr.py + homeassistant/components/sensor/rainbird.py homeassistant/components/sensor/ripple.py homeassistant/components/sensor/sabnzbd.py homeassistant/components/sensor/scrape.py diff --git a/homeassistant/components/rainbird.py b/homeassistant/components/rainbird.py new file mode 100644 index 00000000000..882731d4f2c --- /dev/null +++ b/homeassistant/components/rainbird.py @@ -0,0 +1,47 @@ +""" +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 + +import homeassistant.helpers.config_validation as cv +from homeassistant.const import (CONF_HOST, CONF_PASSWORD) + +REQUIREMENTS = ['pyrainbird==0.1.3'] + +_LOGGER = logging.getLogger(__name__) + +DATA_RAINBIRD = 'rainbird' +DOMAIN = 'rainbird' + +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 componenent.""" + 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) + + initialstatus = controller.currentIrrigation() + if initialstatus == -1: + _LOGGER.error("Error getting state. Possible configuration issues") + return False + + hass.data[DATA_RAINBIRD] = controller + return True diff --git a/homeassistant/components/sensor/rainbird.py b/homeassistant/components/sensor/rainbird.py new file mode 100644 index 00000000000..875e9c37bd3 --- /dev/null +++ b/homeassistant/components/sensor/rainbird.py @@ -0,0 +1,80 @@ +""" +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/sensor.rainbird/ +""" +import logging + +import voluptuous as vol + +from homeassistant.components.rainbird import DATA_RAINBIRD +import homeassistant.helpers.config_validation as cv +from homeassistant.const import CONF_MONITORED_CONDITIONS +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.helpers.entity import Entity + +DEPENDENCIES = ['rainbird'] + +_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_devices, discovery_info=None): + """Set up a Rain Bird sensor.""" + controller = hass.data[DATA_RAINBIRD] + + sensors = [] + for sensor_type in config.get(CONF_MONITORED_CONDITIONS): + sensors.append( + RainBirdSensor(controller, sensor_type)) + + add_devices(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 diff --git a/homeassistant/components/switch/rainbird.py b/homeassistant/components/switch/rainbird.py index c1dbfbc4e72..ee283b3c269 100644 --- a/homeassistant/components/switch/rainbird.py +++ b/homeassistant/components/switch/rainbird.py @@ -2,29 +2,26 @@ 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/ +https://home-assistant.io/components/switch.rainbird/ """ import logging import voluptuous as vol +from homeassistant.components.rainbird import DATA_RAINBIRD from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) -from homeassistant.const import (CONF_PLATFORM, CONF_SWITCHES, CONF_ZONE, +from homeassistant.const import (CONF_SWITCHES, CONF_ZONE, CONF_FRIENDLY_NAME, CONF_TRIGGER_TIME, - CONF_SCAN_INTERVAL, CONF_HOST, CONF_PASSWORD) + CONF_SCAN_INTERVAL) from homeassistant.helpers import config_validation as cv -from homeassistant.exceptions import PlatformNotReady -REQUIREMENTS = ['pyrainbird==0.1.0'] +DEPENDENCIES = ['rainbird'] DOMAIN = 'rainbird' _LOGGER = logging.getLogger(__name__) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_PLATFORM): DOMAIN, - vol.Required(CONF_HOST): cv.string, - vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_SWITCHES, default={}): vol.Schema({ cv.string: { vol.Optional(CONF_FRIENDLY_NAME): cv.string, @@ -38,20 +35,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ 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) - - from pyrainbird import RainbirdController - controller = RainbirdController(_LOGGER) - controller.setConfig(server, password) - - _LOGGER.debug("Rain Bird Controller set to " + str(server)) - - if controller.currentIrrigation() == -1: - _LOGGER.error("Error getting state. Possible configuration issues") - raise PlatformNotReady - else: - _LOGGER.debug("Initialized Rain Bird Controller") + controller = hass.data[DATA_RAINBIRD] devices = [] for dev_id, switch in config.get(CONF_SWITCHES).items(): diff --git a/requirements_all.txt b/requirements_all.txt index e3870c2c934..5eb4361a436 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -799,8 +799,8 @@ pyowm==2.7.1 # homeassistant.components.qwikswitch pyqwikswitch==0.4 -# homeassistant.components.switch.rainbird -pyrainbird==0.1.0 +# homeassistant.components.rainbird +pyrainbird==0.1.3 # homeassistant.components.climate.sensibo pysensibo==1.0.1