mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
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:
parent
f879ac0993
commit
9abdbf3db6
@ -92,6 +92,11 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
|
|||||||
"""Return the name of this sensor including the controller name."""
|
"""Return the name of this sensor including the controller name."""
|
||||||
return "{} online".format(self._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
|
@property
|
||||||
def device_class(self) -> str:
|
def device_class(self) -> str:
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||||
|
@ -13,7 +13,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.auth.util import generate_secret
|
from homeassistant.auth.util import generate_secret
|
||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.const import CONF_API_KEY, EVENT_HOMEASSISTANT_STOP, URL_API
|
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
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
|
||||||
REQUIREMENTS = ['rachiopy==0.1.3']
|
REQUIREMENTS = ['rachiopy==0.1.3']
|
||||||
@ -22,11 +22,19 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DOMAIN = 'rachio'
|
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'
|
CONF_CUSTOM_URL = 'hass_url_override'
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Optional(CONF_CUSTOM_URL): 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)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
@ -112,7 +120,7 @@ def setup(hass, config) -> bool:
|
|||||||
|
|
||||||
# Get the API user
|
# Get the API user
|
||||||
try:
|
try:
|
||||||
person = RachioPerson(hass, rachio)
|
person = RachioPerson(hass, rachio, config[DOMAIN])
|
||||||
except AssertionError as error:
|
except AssertionError as error:
|
||||||
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
||||||
return False
|
return False
|
||||||
@ -126,17 +134,23 @@ def setup(hass, config) -> bool:
|
|||||||
|
|
||||||
# Enable component
|
# Enable component
|
||||||
hass.data[DOMAIN] = person
|
hass.data[DOMAIN] = person
|
||||||
|
|
||||||
|
# Load platforms
|
||||||
|
for component in SUPPORTED_DOMAINS:
|
||||||
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class RachioPerson:
|
class RachioPerson:
|
||||||
"""Represent a Rachio user."""
|
"""Represent a Rachio user."""
|
||||||
|
|
||||||
def __init__(self, hass, rachio):
|
def __init__(self, hass, rachio, config):
|
||||||
"""Create an object from the provided API instance."""
|
"""Create an object from the provided API instance."""
|
||||||
# Use API token to get user ID
|
# Use API token to get user ID
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self.rachio = rachio
|
self.rachio = rachio
|
||||||
|
self.config = config
|
||||||
|
|
||||||
response = rachio.person.getInfo()
|
response = rachio.person.getInfo()
|
||||||
assert int(response[0][KEY_STATUS]) == 200, "API key error"
|
assert int(response[0][KEY_STATUS]) == 200, "API key error"
|
||||||
|
@ -7,10 +7,10 @@ https://home-assistant.io/components/switch.rachio/
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import voluptuous as vol
|
|
||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
from homeassistant.components.switch import SwitchDevice
|
||||||
from homeassistant.components.rachio import (DOMAIN as DOMAIN_RACHIO,
|
from homeassistant.components.rachio import (CONF_MANUAL_RUN_MINS,
|
||||||
|
DOMAIN as DOMAIN_RACHIO,
|
||||||
KEY_DEVICE_ID,
|
KEY_DEVICE_ID,
|
||||||
KEY_ENABLED,
|
KEY_ENABLED,
|
||||||
KEY_ID,
|
KEY_ID,
|
||||||
@ -27,29 +27,20 @@ from homeassistant.components.rachio import (DOMAIN as DOMAIN_RACHIO,
|
|||||||
SUBTYPE_ZONE_COMPLETED,
|
SUBTYPE_ZONE_COMPLETED,
|
||||||
SUBTYPE_SLEEP_MODE_ON,
|
SUBTYPE_SLEEP_MODE_ON,
|
||||||
SUBTYPE_SLEEP_MODE_OFF)
|
SUBTYPE_SLEEP_MODE_OFF)
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.helpers.dispatcher import dispatcher_connect
|
from homeassistant.helpers.dispatcher import dispatcher_connect
|
||||||
|
|
||||||
DEPENDENCIES = ['rachio']
|
DEPENDENCIES = ['rachio']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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_SUMMARY = 'Summary'
|
||||||
ATTR_ZONE_NUMBER = 'Zone number'
|
ATTR_ZONE_NUMBER = 'Zone number'
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Rachio switches."""
|
"""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))
|
_LOGGER.info("Rachio run time is %s", str(manual_run_time))
|
||||||
|
|
||||||
# Add all zones from all controllers as switches
|
# Add all zones from all controllers as switches
|
||||||
@ -126,6 +117,11 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
"""Return the name of the standby switch."""
|
"""Return the name of the standby switch."""
|
||||||
return "{} in standby mode".format(self._controller.name)
|
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
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return an icon for the standby switch."""
|
"""Return an icon for the standby switch."""
|
||||||
@ -189,6 +185,12 @@ class RachioZone(RachioSwitch):
|
|||||||
"""Return the friendly name of the zone."""
|
"""Return the friendly name of the zone."""
|
||||||
return self._zone_name
|
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
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return the icon to display."""
|
"""Return the icon to display."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user