Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions homeassistant/components/binary_sensor/nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
'person_detected',
]

STRUCTURE_BINARY_TYPES = [
'away',
# 'security_state', # wait for pending python-nest update
]

STRUCTURE_BINARY_STATE_MAP = {
'away': {'away': True, 'home': False},
'security_state': {'deter': True, 'ok': False},
}

_BINARY_TYPES_DEPRECATED = [
'hvac_ac_state',
'hvac_aux_heater_state',
Expand All @@ -41,7 +51,7 @@
]

_VALID_BINARY_SENSOR_TYPES = BINARY_TYPES + CLIMATE_BINARY_TYPES \
+ CAMERA_BINARY_TYPES
+ CAMERA_BINARY_TYPES + STRUCTURE_BINARY_TYPES

_LOGGER = logging.getLogger(__name__)

Expand All @@ -68,6 +78,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error(wstr)

sensors = []
for structure in nest.structures():
sensors += [NestBinarySensor(structure, None, variable)
for variable in conditions
if variable in STRUCTURE_BINARY_TYPES]
device_chain = chain(nest.thermostats(),
nest.smoke_co_alarms(),
nest.cameras())
Expand All @@ -88,7 +102,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
sensors += [NestActivityZoneSensor(structure,
device,
activity_zone)]

add_devices(sensors, True)


Expand All @@ -102,7 +115,12 @@ def is_on(self):

def update(self):
"""Retrieve latest state."""
self._state = bool(getattr(self.device, self.variable))
value = getattr(self.device, self.variable)
if self.variable in STRUCTURE_BINARY_TYPES:
self._state = bool(STRUCTURE_BINARY_STATE_MAP
[self.variable][value])
else:
self._state = bool(value)


class NestActivityZoneSensor(NestBinarySensor):
Expand Down
25 changes: 19 additions & 6 deletions homeassistant/components/nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ def __init__(self, hass, conf, nest):
self.local_structure = conf[CONF_STRUCTURE]
_LOGGER.debug("Structures to include: %s", self.local_structure)

def structures(self):
"""Generate a list of structures."""
try:
for structure in self.nest.structures:
if structure.name in self.local_structure:
yield structure
else:
_LOGGER.debug("Ignoring structure %s, not in %s",
structure.name, self.local_structure)
except socket.error:
_LOGGER.error(
"Connection error logging into the nest web service.")

def thermostats(self):
"""Generate a list of thermostats and their location."""
try:
Expand All @@ -188,10 +201,10 @@ def smoke_co_alarms(self):
for structure in self.nest.structures:
if structure.name in self.local_structure:
for device in structure.smoke_co_alarms:
yield(structure, device)
yield (structure, device)
else:
_LOGGER.info("Ignoring structure %s, not in %s",
structure.name, self.local_structure)
_LOGGER.debug("Ignoring structure %s, not in %s",
structure.name, self.local_structure)
except socket.error:
_LOGGER.error(
"Connection error logging into the nest web service.")
Expand All @@ -202,10 +215,10 @@ def cameras(self):
for structure in self.nest.structures:
if structure.name in self.local_structure:
for device in structure.cameras:
yield(structure, device)
yield (structure, device)
else:
_LOGGER.info("Ignoring structure %s, not in %s",
structure.name, self.local_structure)
_LOGGER.debug("Ignoring structure %s, not in %s",
structure.name, self.local_structure)
except socket.error:
_LOGGER.error(
"Connection error logging into the nest web service.")
33 changes: 25 additions & 8 deletions homeassistant/components/sensor/nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@

SENSOR_TEMP_TYPES = ['temperature', 'target']

STRUCTURE_SENSOR_TYPES = ['eta']

VARIABLE_NAME_MAPPING = {'eta': 'eta_begin', 'operation_mode': 'mode'}

_SENSOR_TYPES_DEPRECATED = SENSOR_TYPES_DEPRECATED \
+ list(DEPRECATED_WEATHER_VARS.keys()) + PROTECT_VARS_DEPRECATED

_VALID_SENSOR_TYPES = SENSOR_TYPES + SENSOR_TEMP_TYPES + PROTECT_VARS
_VALID_SENSOR_TYPES = SENSOR_TYPES + SENSOR_TEMP_TYPES + PROTECT_VARS \
+ STRUCTURE_SENSOR_TYPES

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,6 +78,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error(wstr)

all_sensors = []
for structure in nest.structures():
all_sensors += [NestBasicSensor(structure, None, variable)
for variable in conditions
if variable in STRUCTURE_SENSOR_TYPES]
for structure, device in chain(nest.thermostats(), nest.smoke_co_alarms()):
sensors = [NestBasicSensor(structure, device, variable)
for variable in conditions
Expand All @@ -94,13 +103,20 @@ class NestSensor(Entity):
def __init__(self, structure, device, variable):
"""Initialize the sensor."""
self.structure = structure
self.device = device
self.variable = variable

# device specific
self._location = self.device.where
self._name = "{} {}".format(self.device.name_long,
self.variable.replace("_", " "))
if device is not None:
# device specific
self.device = device
self._location = self.device.where
self._name = "{} {}".format(self.device.name_long,
Copy link
Copy Markdown
Contributor

@dgomes dgomes May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from homeassistant.util import slugify

better then using custom format

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slugify is doing replace(" ", "_"), but I am doing replace("_", " ") here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups, you are right 👍

self.variable.replace('_', ' '))
else:
# structure only
self.device = structure
self._name = "{} {}".format(self.structure.name,
self.variable.replace('_', ' '))

self._state = None
self._unit = None

Expand All @@ -127,8 +143,9 @@ def update(self):
"""Retrieve latest state."""
self._unit = SENSOR_UNITS.get(self.variable, None)

if self.variable == 'operation_mode':
self._state = getattr(self.device, "mode")
if self.variable in VARIABLE_NAME_MAPPING:
self._state = getattr(self.device,
VARIABLE_NAME_MAPPING[self.variable])
else:
self._state = getattr(self.device, self.variable)

Expand Down