Add Hydrawise component (#14055)
* Added the Hydrawise component. * Fixed lint errors. * Multiple changes due to review comments addressed. * Simplified boolean test. Passes pylint. * Need hydrawiser package version 0.1.1. * Added a docstring to the device_class method. * Addressed all review comments from MartinHjelmare. * Changed keys to single quote. Removed unnecessary duplicate method. * Removed unused imports. * Changed state to lowercase snakecase. * Changes & fixes from review comments.
This commit is contained in:
parent
fdb250d86c
commit
dfd7ef1fce
6 changed files with 415 additions and 0 deletions
|
@ -123,6 +123,9 @@ omit =
|
|||
homeassistant/components/homematicip_cloud.py
|
||||
homeassistant/components/*/homematicip_cloud.py
|
||||
|
||||
homeassistant/components/hydrawise.py
|
||||
homeassistant/components/*/hydrawise.py
|
||||
|
||||
homeassistant/components/ihc/*
|
||||
homeassistant/components/*/ihc.py
|
||||
|
||||
|
|
81
homeassistant/components/binary_sensor/hydrawise.py
Normal file
81
homeassistant/components/binary_sensor/hydrawise.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
"""
|
||||
Support for Hydrawise sprinkler.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/binary_sensor.hydrawise/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.hydrawise import (
|
||||
BINARY_SENSORS, DATA_HYDRAWISE, HydrawiseEntity, DEVICE_MAP,
|
||||
DEVICE_MAP_INDEX)
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
||||
|
||||
DEPENDENCIES = ['hydrawise']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=BINARY_SENSORS):
|
||||
vol.All(cv.ensure_list, [vol.In(BINARY_SENSORS)]),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a sensor for a Hydrawise device."""
|
||||
hydrawise = hass.data[DATA_HYDRAWISE].data
|
||||
|
||||
sensors = []
|
||||
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
||||
if sensor_type in ['status', 'rain_sensor']:
|
||||
sensors.append(
|
||||
HydrawiseBinarySensor(
|
||||
hydrawise.controller_status, sensor_type))
|
||||
|
||||
else:
|
||||
# create a sensor for each zone
|
||||
for zone in hydrawise.relays:
|
||||
zone_data = zone
|
||||
zone_data['running'] = \
|
||||
hydrawise.controller_status.get('running', False)
|
||||
sensors.append(HydrawiseBinarySensor(zone_data, sensor_type))
|
||||
|
||||
add_devices(sensors, True)
|
||||
|
||||
|
||||
class HydrawiseBinarySensor(HydrawiseEntity, BinarySensorDevice):
|
||||
"""A sensor implementation for Hydrawise device."""
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data and updates the state."""
|
||||
_LOGGER.debug("Updating Hydrawise binary sensor: %s", self._name)
|
||||
mydata = self.hass.data[DATA_HYDRAWISE].data
|
||||
if self._sensor_type == 'status':
|
||||
self._state = mydata.status == 'All good!'
|
||||
elif self._sensor_type == 'rain_sensor':
|
||||
for sensor in mydata.sensors:
|
||||
if sensor['name'] == 'Rain':
|
||||
self._state = sensor['active'] == 1
|
||||
elif self._sensor_type == 'is_watering':
|
||||
if not mydata.running:
|
||||
self._state = False
|
||||
elif int(mydata.running[0]['relay']) == self.data['relay']:
|
||||
self._state = True
|
||||
else:
|
||||
self._state = False
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class of the sensor type."""
|
||||
return DEVICE_MAP[self._sensor_type][
|
||||
DEVICE_MAP_INDEX.index('DEVICE_CLASS_INDEX')]
|
153
homeassistant/components/hydrawise.py
Normal file
153
homeassistant/components/hydrawise.py
Normal file
|
@ -0,0 +1,153 @@
|
|||
"""
|
||||
Support for Hydrawise cloud.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/hydrawise/
|
||||
"""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from requests.exceptions import ConnectTimeout, HTTPError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ATTRIBUTION, CONF_ACCESS_TOKEN, CONF_SCAN_INTERVAL)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect, dispatcher_send)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import track_time_interval
|
||||
|
||||
REQUIREMENTS = ['hydrawiser==0.1.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60]
|
||||
|
||||
CONF_ATTRIBUTION = "Data provided by hydrawise.com"
|
||||
CONF_WATERING_TIME = 'watering_minutes'
|
||||
|
||||
NOTIFICATION_ID = 'hydrawise_notification'
|
||||
NOTIFICATION_TITLE = 'Hydrawise Setup'
|
||||
|
||||
DATA_HYDRAWISE = 'hydrawise'
|
||||
DOMAIN = 'hydrawise'
|
||||
DEFAULT_WATERING_TIME = 15
|
||||
|
||||
DEVICE_MAP_INDEX = ['KEY_INDEX', 'ICON_INDEX', 'DEVICE_CLASS_INDEX',
|
||||
'UNIT_OF_MEASURE_INDEX']
|
||||
DEVICE_MAP = {
|
||||
'auto_watering': ['Automatic Watering', 'mdi:autorenew', '', ''],
|
||||
'is_watering': ['Watering', '', 'moisture', ''],
|
||||
'manual_watering': ['Manual Watering', 'mdi:water-pump', '', ''],
|
||||
'next_cycle': ['Next Cycle', 'mdi:calendar-clock', '', ''],
|
||||
'status': ['Status', '', 'connectivity', ''],
|
||||
'watering_time': ['Watering Time', 'mdi:water-pump', '', 'min'],
|
||||
'rain_sensor': ['Rain Sensor', '', 'moisture', '']
|
||||
}
|
||||
|
||||
BINARY_SENSORS = ['is_watering', 'status', 'rain_sensor']
|
||||
|
||||
SENSORS = ['next_cycle', 'watering_time']
|
||||
|
||||
SWITCHES = ['auto_watering', 'manual_watering']
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
SIGNAL_UPDATE_HYDRAWISE = "hydrawise_update"
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
||||
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL):
|
||||
cv.time_period,
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Set up the Hunter Hydrawise component."""
|
||||
conf = config[DOMAIN]
|
||||
access_token = conf[CONF_ACCESS_TOKEN]
|
||||
scan_interval = conf.get(CONF_SCAN_INTERVAL)
|
||||
|
||||
try:
|
||||
from hydrawiser.core import Hydrawiser
|
||||
|
||||
hydrawise = Hydrawiser(user_token=access_token)
|
||||
hass.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise)
|
||||
except (ConnectTimeout, HTTPError) as ex:
|
||||
_LOGGER.error(
|
||||
"Unable to connect to Hydrawise cloud service: %s", str(ex))
|
||||
hass.components.persistent_notification.create(
|
||||
'Error: {}<br />'
|
||||
'You will need to restart hass after fixing.'
|
||||
''.format(ex),
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID)
|
||||
return False
|
||||
|
||||
def hub_refresh(event_time):
|
||||
"""Call Hydrawise hub to refresh information."""
|
||||
_LOGGER.debug("Updating Hydrawise Hub component")
|
||||
hass.data[DATA_HYDRAWISE].data.update_controller_info()
|
||||
dispatcher_send(hass, SIGNAL_UPDATE_HYDRAWISE)
|
||||
|
||||
# Call the Hydrawise API to refresh updates
|
||||
track_time_interval(hass, hub_refresh, scan_interval)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class HydrawiseHub(object):
|
||||
"""Representation of a base Hydrawise device."""
|
||||
|
||||
def __init__(self, data):
|
||||
"""Initialize the entity."""
|
||||
self.data = data
|
||||
|
||||
|
||||
class HydrawiseEntity(Entity):
|
||||
"""Entity class for Hydrawise devices."""
|
||||
|
||||
def __init__(self, data, sensor_type):
|
||||
"""Initialize the Hydrawise entity."""
|
||||
self.data = data
|
||||
self._sensor_type = sensor_type
|
||||
self._name = "{0} {1}".format(
|
||||
self.data['name'],
|
||||
DEVICE_MAP[self._sensor_type][
|
||||
DEVICE_MAP_INDEX.index('KEY_INDEX')])
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
async_dispatcher_connect(
|
||||
self.hass, SIGNAL_UPDATE_HYDRAWISE, self._update_callback)
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
"""Call update method."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the units of measurement."""
|
||||
return DEVICE_MAP[self._sensor_type][
|
||||
DEVICE_MAP_INDEX.index('UNIT_OF_MEASURE_INDEX')]
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {
|
||||
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
|
||||
'identifier': self.data.get('relay'),
|
||||
}
|
72
homeassistant/components/sensor/hydrawise.py
Normal file
72
homeassistant/components/sensor/hydrawise.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
"""
|
||||
Support for Hydrawise sprinkler.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.hydrawise/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.hydrawise import (
|
||||
DATA_HYDRAWISE, HydrawiseEntity, DEVICE_MAP, DEVICE_MAP_INDEX, SENSORS)
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
||||
|
||||
DEPENDENCIES = ['hydrawise']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSORS):
|
||||
vol.All(cv.ensure_list, [vol.In(SENSORS)]),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a sensor for a Hydrawise device."""
|
||||
hydrawise = hass.data[DATA_HYDRAWISE].data
|
||||
|
||||
sensors = []
|
||||
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
||||
for zone in hydrawise.relays:
|
||||
sensors.append(HydrawiseSensor(zone, sensor_type))
|
||||
|
||||
add_devices(sensors, True)
|
||||
|
||||
|
||||
class HydrawiseSensor(HydrawiseEntity):
|
||||
"""A sensor implementation for Hydrawise device."""
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data and updates the states."""
|
||||
mydata = self.hass.data[DATA_HYDRAWISE].data
|
||||
_LOGGER.debug("Updating Hydrawise sensor: %s", self._name)
|
||||
if self._sensor_type == 'watering_time':
|
||||
if not mydata.running:
|
||||
self._state = 0
|
||||
else:
|
||||
if int(mydata.running[0]['relay']) == self.data['relay']:
|
||||
self._state = int(mydata.running[0]['time_left']/60)
|
||||
else:
|
||||
self._state = 0
|
||||
else: # _sensor_type == 'next_cycle'
|
||||
for relay in mydata.relays:
|
||||
if relay['relay'] == self.data['relay']:
|
||||
if relay['nicetime'] == 'Not scheduled':
|
||||
self._state = 'not_scheduled'
|
||||
else:
|
||||
self._state = relay['nicetime'].split(',')[0] + \
|
||||
' ' + relay['nicetime'].split(' ')[3]
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return DEVICE_MAP[self._sensor_type][
|
||||
DEVICE_MAP_INDEX.index('ICON_INDEX')]
|
103
homeassistant/components/switch/hydrawise.py
Normal file
103
homeassistant/components/switch/hydrawise.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
"""
|
||||
Support for Hydrawise cloud.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.hydrawise/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.hydrawise import (
|
||||
ALLOWED_WATERING_TIME, CONF_WATERING_TIME,
|
||||
DATA_HYDRAWISE, DEFAULT_WATERING_TIME, HydrawiseEntity, SWITCHES,
|
||||
DEVICE_MAP, DEVICE_MAP_INDEX)
|
||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
||||
|
||||
DEPENDENCIES = ['hydrawise']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=SWITCHES):
|
||||
vol.All(cv.ensure_list, [vol.In(SWITCHES)]),
|
||||
vol.Optional(CONF_WATERING_TIME, default=DEFAULT_WATERING_TIME):
|
||||
vol.All(vol.In(ALLOWED_WATERING_TIME)),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a sensor for a Hydrawise device."""
|
||||
hydrawise = hass.data[DATA_HYDRAWISE].data
|
||||
|
||||
default_watering_timer = config.get(CONF_WATERING_TIME)
|
||||
|
||||
sensors = []
|
||||
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
||||
# create a switch for each zone
|
||||
for zone in hydrawise.relays:
|
||||
sensors.append(
|
||||
HydrawiseSwitch(default_watering_timer,
|
||||
zone,
|
||||
sensor_type))
|
||||
|
||||
add_devices(sensors, True)
|
||||
|
||||
|
||||
class HydrawiseSwitch(HydrawiseEntity, SwitchDevice):
|
||||
"""A switch implementation for Hydrawise device."""
|
||||
|
||||
def __init__(self, default_watering_timer, *args):
|
||||
"""Initialize a switch for Hydrawise device."""
|
||||
super().__init__(*args)
|
||||
self._default_watering_timer = default_watering_timer
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._state
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
if self._sensor_type == 'manual_watering':
|
||||
self.hass.data[DATA_HYDRAWISE].data.run_zone(
|
||||
self._default_watering_timer, (self.data['relay']-1))
|
||||
elif self._sensor_type == 'auto_watering':
|
||||
self.hass.data[DATA_HYDRAWISE].data.suspend_zone(
|
||||
0, (self.data['relay']-1))
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
if self._sensor_type == 'manual_watering':
|
||||
self.hass.data[DATA_HYDRAWISE].data.run_zone(
|
||||
0, (self.data['relay']-1))
|
||||
elif self._sensor_type == 'auto_watering':
|
||||
self.hass.data[DATA_HYDRAWISE].data.suspend_zone(
|
||||
365, (self.data['relay']-1))
|
||||
|
||||
def update(self):
|
||||
"""Update device state."""
|
||||
mydata = self.hass.data[DATA_HYDRAWISE].data
|
||||
_LOGGER.debug("Updating Hydrawise switch: %s", self._name)
|
||||
if self._sensor_type == 'manual_watering':
|
||||
if not mydata.running:
|
||||
self._state = False
|
||||
else:
|
||||
self._state = int(
|
||||
mydata.running[0]['relay']) == self.data['relay']
|
||||
elif self._sensor_type == 'auto_watering':
|
||||
for relay in mydata.relays:
|
||||
if relay['relay'] == self.data['relay']:
|
||||
if relay.get('suspended') is not None:
|
||||
self._state = False
|
||||
else:
|
||||
self._state = True
|
||||
break
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend, if any."""
|
||||
return DEVICE_MAP[self._sensor_type][
|
||||
DEVICE_MAP_INDEX.index('ICON_INDEX')]
|
|
@ -430,6 +430,9 @@ https://github.com/soldag/pyflic/archive/0.4.zip#pyflic==0.4
|
|||
# homeassistant.components.media_player.lg_netcast
|
||||
https://github.com/wokar/pylgnetcast/archive/v0.2.0.zip#pylgnetcast==0.2.0
|
||||
|
||||
# homeassistant.components.hydrawise
|
||||
hydrawiser==0.1.1
|
||||
|
||||
# homeassistant.components.sensor.bh1750
|
||||
# homeassistant.components.sensor.bme280
|
||||
# homeassistant.components.sensor.htu21d
|
||||
|
|
Loading…
Add table
Reference in a new issue