Rachio component modernization (#16911)

Add `unique_id` to all rachio entities
Add platform discovery to rachio component
Move config options from switch.rachio platform to the rachio component
This commit is contained in:
Greg Laabs 2018-09-27 14:17:15 -07:00 committed by Paulus Schoutsen
parent f879ac0993
commit 9abdbf3db6
3 changed files with 38 additions and 17 deletions

View File

@ -92,6 +92,11 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
"""Return the name of this sensor including the controller name."""
return "{} online".format(self._controller.name)
@property
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return "{}-online".format(self._controller.controller_id)
@property
def device_class(self) -> str:
"""Return the class of this device, from component DEVICE_CLASSES."""

View File

@ -13,7 +13,7 @@ import voluptuous as vol
from homeassistant.auth.util import generate_secret
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import CONF_API_KEY, EVENT_HOMEASSISTANT_STOP, URL_API
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import discovery, config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
REQUIREMENTS = ['rachiopy==0.1.3']
@ -22,11 +22,19 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = 'rachio'
SUPPORTED_DOMAINS = ['switch', 'binary_sensor']
# Manual run length
CONF_MANUAL_RUN_MINS = 'manual_run_mins'
DEFAULT_MANUAL_RUN_MINS = 10
CONF_CUSTOM_URL = 'hass_url_override'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_CUSTOM_URL): cv.string,
vol.Optional(CONF_MANUAL_RUN_MINS, default=DEFAULT_MANUAL_RUN_MINS):
cv.positive_int,
})
}, extra=vol.ALLOW_EXTRA)
@ -112,7 +120,7 @@ def setup(hass, config) -> bool:
# Get the API user
try:
person = RachioPerson(hass, rachio)
person = RachioPerson(hass, rachio, config[DOMAIN])
except AssertionError as error:
_LOGGER.error("Could not reach the Rachio API: %s", error)
return False
@ -126,17 +134,23 @@ def setup(hass, config) -> bool:
# Enable component
hass.data[DOMAIN] = person
# Load platforms
for component in SUPPORTED_DOMAINS:
discovery.load_platform(hass, component, DOMAIN, {}, config)
return True
class RachioPerson:
"""Represent a Rachio user."""
def __init__(self, hass, rachio):
def __init__(self, hass, rachio, config):
"""Create an object from the provided API instance."""
# Use API token to get user ID
self._hass = hass
self.rachio = rachio
self.config = config
response = rachio.person.getInfo()
assert int(response[0][KEY_STATUS]) == 200, "API key error"

View File

@ -7,10 +7,10 @@ https://home-assistant.io/components/switch.rachio/
from abc import abstractmethod
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.components.rachio import (DOMAIN as DOMAIN_RACHIO,
from homeassistant.components.switch import SwitchDevice
from homeassistant.components.rachio import (CONF_MANUAL_RUN_MINS,
DOMAIN as DOMAIN_RACHIO,
KEY_DEVICE_ID,
KEY_ENABLED,
KEY_ID,
@ -27,29 +27,20 @@ from homeassistant.components.rachio import (DOMAIN as DOMAIN_RACHIO,
SUBTYPE_ZONE_COMPLETED,
SUBTYPE_SLEEP_MODE_ON,
SUBTYPE_SLEEP_MODE_OFF)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_connect
DEPENDENCIES = ['rachio']
_LOGGER = logging.getLogger(__name__)
# Manual run length
CONF_MANUAL_RUN_MINS = 'manual_run_mins'
DEFAULT_MANUAL_RUN_MINS = 10
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_MANUAL_RUN_MINS, default=DEFAULT_MANUAL_RUN_MINS):
cv.positive_int,
})
ATTR_ZONE_SUMMARY = 'Summary'
ATTR_ZONE_NUMBER = 'Zone number'
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Rachio switches."""
manual_run_time = timedelta(minutes=config.get(CONF_MANUAL_RUN_MINS))
manual_run_time = timedelta(minutes=hass.data[DOMAIN_RACHIO].config.get(
CONF_MANUAL_RUN_MINS))
_LOGGER.info("Rachio run time is %s", str(manual_run_time))
# Add all zones from all controllers as switches
@ -126,6 +117,11 @@ class RachioStandbySwitch(RachioSwitch):
"""Return the name of the standby switch."""
return "{} in standby mode".format(self._controller.name)
@property
def unique_id(self) -> str:
"""Return a unique id by combinining controller id and purpose."""
return "{}-standby".format(self._controller.controller_id)
@property
def icon(self) -> str:
"""Return an icon for the standby switch."""
@ -189,6 +185,12 @@ class RachioZone(RachioSwitch):
"""Return the friendly name of the zone."""
return self._zone_name
@property
def unique_id(self) -> str:
"""Return a unique id by combinining controller id and zone number."""
return "{}-zone-{}".format(self._controller.controller_id,
self.zone_id)
@property
def icon(self) -> str:
"""Return the icon to display."""