2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise cloud."""
|
2018-05-26 16:42:52 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-12-04 13:09:35 +00:00
|
|
|
from hydrawiser.core import Hydrawiser
|
2018-05-26 16:42:52 +00:00
|
|
|
from requests.exceptions import ConnectTimeout, HTTPError
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-06-21 21:55:47 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
DEVICE_CLASS_MOISTURE,
|
|
|
|
)
|
|
|
|
from homeassistant.components.sensor import DEVICE_CLASS_TIMESTAMP
|
|
|
|
from homeassistant.components.switch import DEVICE_CLASS_SWITCH
|
2020-02-23 20:09:24 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_ACCESS_TOKEN,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
TIME_MINUTES,
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
from homeassistant.core import callback
|
2019-12-04 13:09:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
|
2018-05-26 16:42:52 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.event import track_time_interval
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60]
|
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by hydrawise.com"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_WATERING_TIME = "watering_minutes"
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
NOTIFICATION_ID = "hydrawise_notification"
|
|
|
|
NOTIFICATION_TITLE = "Hydrawise Setup"
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_HYDRAWISE = "hydrawise"
|
|
|
|
DOMAIN = "hydrawise"
|
2018-05-26 16:42:52 +00:00
|
|
|
DEFAULT_WATERING_TIME = 15
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_MAP_INDEX = [
|
|
|
|
"KEY_INDEX",
|
|
|
|
"ICON_INDEX",
|
|
|
|
"DEVICE_CLASS_INDEX",
|
|
|
|
"UNIT_OF_MEASURE_INDEX",
|
|
|
|
]
|
2018-05-26 16:42:52 +00:00
|
|
|
DEVICE_MAP = {
|
2020-06-21 21:55:47 +00:00
|
|
|
"auto_watering": ["Automatic Watering", None, DEVICE_CLASS_SWITCH, None],
|
|
|
|
"is_watering": ["Watering", None, DEVICE_CLASS_MOISTURE, None],
|
|
|
|
"manual_watering": ["Manual Watering", None, DEVICE_CLASS_SWITCH, None],
|
|
|
|
"next_cycle": ["Next Cycle", None, DEVICE_CLASS_TIMESTAMP, None],
|
|
|
|
"status": ["Status", None, DEVICE_CLASS_CONNECTIVITY, None],
|
|
|
|
"watering_time": ["Watering Time", "mdi:water-pump", None, TIME_MINUTES],
|
2018-05-26 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 21:55:47 +00:00
|
|
|
BINARY_SENSORS = ["is_watering", "status"]
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSORS = ["next_cycle", "watering_time"]
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SWITCHES = ["auto_watering", "manual_watering"]
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
|
|
|
|
|
|
|
SIGNAL_UPDATE_HYDRAWISE = "hydrawise_update"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
hydrawise = Hydrawiser(user_token=access_token)
|
|
|
|
hass.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise)
|
|
|
|
except (ConnectTimeout, HTTPError) as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unable to connect to Hydrawise cloud service: %s", str(ex))
|
2018-05-26 16:42:52 +00:00
|
|
|
hass.components.persistent_notification.create(
|
2020-02-28 11:39:29 +00:00
|
|
|
f"Error: {ex}<br />You will need to restart hass after fixing.",
|
2018-05-26 16:42:52 +00:00
|
|
|
title=NOTIFICATION_TITLE,
|
2019-07-31 19:25:30 +00:00
|
|
|
notification_id=NOTIFICATION_ID,
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class HydrawiseHub:
|
2018-05-26 16:42:52 +00:00
|
|
|
"""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
|
2020-02-28 11:39:29 +00:00
|
|
|
self._name = f"{self.data['name']} {DEVICE_MAP[self._sensor_type][DEVICE_MAP_INDEX.index('KEY_INDEX')]}"
|
2018-05-26 16:42:52 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-05-26 16:42:52 +00:00
|
|
|
"""Register callbacks."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_HYDRAWISE, self._update_callback
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-05-26 16:42:52 +00:00
|
|
|
"""Return the state attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {ATTR_ATTRIBUTION: ATTRIBUTION, "identifier": self.data.get("relay")}
|
2020-06-21 21:55:47 +00:00
|
|
|
|
|
|
|
@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")
|
|
|
|
]
|
|
|
|
|
|
|
|
@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")]
|