diff --git a/homeassistant/components/binary_sensor/rachio.py b/homeassistant/components/binary_sensor/rachio.py index 798b6a754d1..36a32c79c5c 100644 --- a/homeassistant/components/binary_sensor/rachio.py +++ b/homeassistant/components/binary_sensor/rachio.py @@ -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.""" diff --git a/homeassistant/components/rachio.py b/homeassistant/components/rachio.py index cd80b7bec9b..27827da0182 100644 --- a/homeassistant/components/rachio.py +++ b/homeassistant/components/rachio.py @@ -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" diff --git a/homeassistant/components/switch/rachio.py b/homeassistant/components/switch/rachio.py index 956befeeb9f..4797aae9a8c 100644 --- a/homeassistant/components/switch/rachio.py +++ b/homeassistant/components/switch/rachio.py @@ -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."""