Refactory nest component/platforms (#4219)

* Refactory nest component/platforms
This commit is contained in:
Pascal Vizeli authored and William Scanlon committed 2016-11-04 20:22:47 -04:00
1 parent a3db0ec231
commit 91227d9a2e
4 files changed
+178 -108

No files matched your search

+47 -42
View File
@@ -18,9 +18,8 @@ REQUIREMENTS = ['python-nest==2.11.0']
DOMAIN = 'nest'
NEST = None
DATA_NEST = 'nest'
STRUCTURES_TO_INCLUDE = None
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
@@ -31,52 +30,58 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
def devices():
"""Generator returning list of devices and their location."""
try:
for structure in NEST.structures:
if structure.name in STRUCTURES_TO_INCLUDE:
for device in structure.devices:
yield (structure, device)
else:
_LOGGER.debug("Ignoring structure %s, not in %s",
structure.name, STRUCTURES_TO_INCLUDE)
except socket.error:
_LOGGER.error("Connection error logging into the nest web service.")
def protect_devices():
"""Generator returning list of protect devices."""
try:
for structure in NEST.structures:
if structure.name in STRUCTURES_TO_INCLUDE:
for device in structure.protectdevices:
yield(structure, device)
else:
_LOGGER.info("Ignoring structure %s, not in %s",
structure.name, STRUCTURES_TO_INCLUDE)
except socket.error:
_LOGGER.error("Connection error logging into the nest web service.")
# pylint: disable=unused-argument
def setup(hass, config):
"""Setup the Nest thermostat component."""
global NEST
global STRUCTURES_TO_INCLUDE
import nest
conf = config[DOMAIN]
username = conf[CONF_USERNAME]
password = conf[CONF_PASSWORD]
import nest
nest = nest.Nest(username, password)
hass.data[DATA_NEST] = NestDevice(hass, conf, nest)
NEST = nest.Nest(username, password)
if CONF_STRUCTURE not in conf:
STRUCTURES_TO_INCLUDE = [s.name for s in NEST.structures]
else:
STRUCTURES_TO_INCLUDE = conf[CONF_STRUCTURE]
_LOGGER.debug("Structures to include: %s", STRUCTURES_TO_INCLUDE)
return True
class NestDevice(object):
"""Structure Nest functions for hass."""
def __init__(self, hass, conf, nest):
"""Init Nest Devices."""
self.hass = hass
self.nest = nest
if CONF_STRUCTURE not in conf:
self._structure = [s.name for s in nest.structures]
else:
self._structure = conf[CONF_STRUCTURE]
_LOGGER.debug("Structures to include: %s", self._structure)
def devices(self):
"""Generator returning list of devices and their location."""
try:
for structure in self.nest.structures:
if structure.name in self._structure:
for device in structure.devices:
yield (structure, device)
else:
_LOGGER.debug("Ignoring structure %s, not in %s",
structure.name, self._structure)
except socket.error:
_LOGGER.error(
"Connection error logging into the nest web service.")
def protect_devices(self):
"""Generator returning list of protect devices."""
try:
for structure in self.nest.structures:
if structure.name in self._structure:
for device in structure.protectdevices:
yield(structure, device)
else:
_LOGGER.info("Ignoring structure %s, not in %s",
structure.name, self._structure)
except socket.error:
_LOGGER.error(
"Connection error logging into the nest web service.")