From a144862e0e1904f5f6324dca6c7d1f3fd3c19fd2 Mon Sep 17 00:00:00 2001 From: Matthew Schick Date: Tue, 16 May 2017 13:57:34 -0400 Subject: [PATCH 1/2] Add service to set nest away/home modes * New service `nest.set_mode` * Update the NestDevice object to export the local structures --- homeassistant/components/nest.py | 44 +++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/nest.py b/homeassistant/components/nest.py index 8d99a4d0327d3..7ec638ecf98ab 100644 --- a/homeassistant/components/nest.py +++ b/homeassistant/components/nest.py @@ -29,10 +29,18 @@ CONF_CLIENT_ID = 'client_id' CONF_CLIENT_SECRET = 'client_secret' +ATTR_HOME_MODE = 'home_mode' +ATTR_STRUCTURE = 'structure' + SENSOR_SCHEMA = vol.Schema({ vol.Optional(CONF_MONITORED_CONDITIONS): vol.All(cv.ensure_list) }) +AWAY_SCHEMA = vol.Schema({ + vol.Required(ATTR_HOME_MODE): cv.string, + vol.Optional(ATTR_STRUCTURE): vol.All(cv.ensure_list) +}) + CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ vol.Required(CONF_CLIENT_ID): cv.string, @@ -126,6 +134,24 @@ def setup(hass, config): client_id=client_id, client_secret=client_secret) setup_nest(hass, nest, config) + def set_mode(service): + """Set the home/away mode for a Nest structure.""" + if 'structure' in service.data: + structures = service.data['structure'] + else: + structures = hass.data[DATA_NEST].local_structure + + for structure in nest.structures: + if structure.name in structures: + _LOGGER.info("Setting mode for %s", structure.name) + structure.away = service.data['home_mode'] + else: + _LOGGER.error("Invalid structure %s", + service.data['structure']) + + hass.services.register( + DOMAIN, 'set_mode', set_mode, schema=AWAY_SCHEMA) + return True @@ -138,21 +164,21 @@ def __init__(self, hass, conf, nest): self.nest = nest if CONF_STRUCTURE not in conf: - self._structure = [s.name for s in nest.structures] + self.local_structure = [s.name for s in nest.structures] else: - self._structure = conf[CONF_STRUCTURE] - _LOGGER.debug("Structures to include: %s", self._structure) + self.local_structure = conf[CONF_STRUCTURE] + _LOGGER.debug("Structures to include: %s", self.local_structure) def thermostats(self): """Generate a list of thermostats and their location.""" try: for structure in self.nest.structures: - if structure.name in self._structure: + if structure.name in self.local_structure: for device in structure.thermostats: yield (structure, device) else: _LOGGER.debug("Ignoring structure %s, not in %s", - structure.name, self._structure) + structure.name, self.local_structure) except socket.error: _LOGGER.error( "Connection error logging into the nest web service.") @@ -161,12 +187,12 @@ def smoke_co_alarms(self): """Generate a list of smoke co alarams.""" try: for structure in self.nest.structures: - if structure.name in self._structure: + if structure.name in self.local_structure: for device in structure.smoke_co_alarms: yield(structure, device) else: _LOGGER.info("Ignoring structure %s, not in %s", - structure.name, self._structure) + structure.name, self.local_structure) except socket.error: _LOGGER.error( "Connection error logging into the nest web service.") @@ -175,12 +201,12 @@ def cameras(self): """Generate a list of cameras.""" try: for structure in self.nest.structures: - if structure.name in self._structure: + if structure.name in self.local_structure: for device in structure.cameras: yield(structure, device) else: _LOGGER.info("Ignoring structure %s, not in %s", - structure.name, self._structure) + structure.name, self.local_structure) except socket.error: _LOGGER.error( "Connection error logging into the nest web service.") From 0293a621f88e7e70b0bce44fc658cbcb92baaf81 Mon Sep 17 00:00:00 2001 From: Matthew Schick Date: Thu, 1 Jun 2017 10:00:53 -0400 Subject: [PATCH 2/2] Validation and structure cleanup --- homeassistant/components/nest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/nest.py b/homeassistant/components/nest.py index 7ec638ecf98ab..6443fc47a8501 100644 --- a/homeassistant/components/nest.py +++ b/homeassistant/components/nest.py @@ -38,7 +38,7 @@ AWAY_SCHEMA = vol.Schema({ vol.Required(ATTR_HOME_MODE): cv.string, - vol.Optional(ATTR_STRUCTURE): vol.All(cv.ensure_list) + vol.Optional(ATTR_STRUCTURE): vol.All(cv.ensure_list, cv.string) }) CONFIG_SCHEMA = vol.Schema({ @@ -136,18 +136,18 @@ def setup(hass, config): def set_mode(service): """Set the home/away mode for a Nest structure.""" - if 'structure' in service.data: - structures = service.data['structure'] + if ATTR_STRUCTURE in service.data: + structures = service.data[ATTR_STRUCTURE] else: structures = hass.data[DATA_NEST].local_structure for structure in nest.structures: if structure.name in structures: _LOGGER.info("Setting mode for %s", structure.name) - structure.away = service.data['home_mode'] + structure.away = service.data[ATTR_HOME_MODE] else: _LOGGER.error("Invalid structure %s", - service.data['structure']) + service.data[ATTR_STRUCTURE]) hass.services.register( DOMAIN, 'set_mode', set_mode, schema=AWAY_SCHEMA)