Make nx584 expose zone types (sensor classes)

With this, plus https://github.com/balloob/home-assistant-polymer/pull/32,
I can have nx584 sensors use a proper icon in the UI.
This commit is contained in:
Dan Smith 2016-02-17 16:00:01 -08:00
parent 2d932f89fc
commit d93883f153
2 changed files with 26 additions and 9 deletions

View file

@ -12,7 +12,8 @@ import time
import requests
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.binary_sensor import (BinarySensorDevice,
SENSOR_CLASSES)
REQUIREMENTS = ['pynx584==0.2']
_LOGGER = logging.getLogger(__name__)
@ -24,11 +25,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
host = config.get('host', 'localhost:5007')
exclude = config.get('exclude_zones', [])
zone_types = config.get('zone_types', {})
if not all(isinstance(zone, int) for zone in exclude):
_LOGGER.error('Invalid excluded zone specified (use zone number)')
return False
if not all(isinstance(zone, int) and ztype in SENSOR_CLASSES
for zone, ztype in zone_types.items()):
_LOGGER.error('Invalid zone_types entry')
return False
try:
client = nx584_client.Client('http://%s' % host)
zones = client.list_zones()
@ -42,7 +49,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False
zone_sensors = {
zone['number']: NX584ZoneSensor(zone)
zone['number']: NX584ZoneSensor(
zone,
zone_types.get(zone['number'], 'opening'))
for zone in zones
if zone['number'] not in exclude}
if zone_sensors:
@ -58,8 +67,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class NX584ZoneSensor(BinarySensorDevice):
"""Represents a NX584 zone as a sensor."""
def __init__(self, zone):
def __init__(self, zone, zone_type):
self._zone = zone
self._zone_type = zone_type
@property
def sensor_class(self):
return self._zone_type
@property
def should_poll(self):

View file

@ -41,7 +41,7 @@ class TestNX584SensorSetup(unittest.TestCase):
hass = mock.MagicMock()
self.assertTrue(nx584.setup_platform(hass, {}, add_devices))
mock_nx.assert_has_calls([
mock.call(zone)
mock.call(zone, 'opening')
for zone in self.fake_zones])
self.assertTrue(add_devices.called)
nx584_client.Client.assert_called_once_with('http://localhost:5007')
@ -58,8 +58,8 @@ class TestNX584SensorSetup(unittest.TestCase):
hass = mock.MagicMock()
self.assertTrue(nx584.setup_platform(hass, config, add_devices))
mock_nx.assert_has_calls([
mock.call(self.fake_zones[0]),
mock.call(self.fake_zones[2]),
mock.call(self.fake_zones[0], 'opening'),
mock.call(self.fake_zones[2], 'motion'),
])
self.assertTrue(add_devices.called)
nx584_client.Client.assert_called_once_with('http://foo:123')
@ -74,6 +74,9 @@ class TestNX584SensorSetup(unittest.TestCase):
def test_setup_bad_config(self):
bad_configs = [
{'exclude_zones': ['a']},
{'zone_types': {'a': 'b'}},
{'zone_types': {1: 'notatype'}},
{'zone_types': {'notazone': 'motion'}},
]
for config in bad_configs:
self._test_assert_graceful_fail(config)
@ -98,7 +101,7 @@ class TestNX584SensorSetup(unittest.TestCase):
class TestNX584ZoneSensor(unittest.TestCase):
def test_sensor_normal(self):
zone = {'number': 1, 'name': 'foo', 'state': True}
sensor = nx584.NX584ZoneSensor(zone)
sensor = nx584.NX584ZoneSensor(zone, 'motion')
self.assertEqual('foo', sensor.name)
self.assertFalse(sensor.should_poll)
self.assertTrue(sensor.is_on)
@ -113,8 +116,8 @@ class TestNX584Watcher(unittest.TestCase):
zone1 = {'number': 1, 'name': 'foo', 'state': True}
zone2 = {'number': 2, 'name': 'bar', 'state': True}
zones = {
1: nx584.NX584ZoneSensor(zone1),
2: nx584.NX584ZoneSensor(zone2),
1: nx584.NX584ZoneSensor(zone1, 'motion'),
2: nx584.NX584ZoneSensor(zone2, 'motion'),
}
watcher = nx584.NX584Watcher(None, zones)
watcher._process_zone_event({'zone': 1, 'zone_state': False})