From 8dbda5a54efecad2ac65b1381765e7eb5e693b7a Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 21 Aug 2018 23:30:56 +0200 Subject: [PATCH 01/14] Support device registry --- .../components/binary_sensor/deconz.py | 18 ++++++++++ homeassistant/components/deconz/__init__.py | 9 ++++- homeassistant/components/light/deconz.py | 18 ++++++++++ homeassistant/components/sensor/deconz.py | 36 +++++++++++++++++++ homeassistant/components/switch/deconz.py | 18 ++++++++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 0a370d754eea4d..6edb7c6a30ec42 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -113,3 +113,21 @@ def device_state_attributes(self): if self._sensor.type in PRESENCE and self._sensor.dark is not None: attr[ATTR_DARK] = self._sensor.dark return attr + + @property + def device(self): + """Description for device registry.""" + if (self._sensor.uniqueid is not None and + self._sensor.uniqueid.count(':') == 7): + serial = self._sensor.uniqueid.split('-', 1)[0] + else: + return None + dev = { + 'identifiers': [['serial', serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'connection': [['Zigbee', serial]], + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } + return dev diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index a0b2b5a23cb389..63cb915c8d2b94 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -23,7 +23,7 @@ CONF_ALLOW_CLIP_SENSOR, CONFIG_FILE, DATA_DECONZ_EVENT, DATA_DECONZ_ID, DATA_DECONZ_UNSUB, DOMAIN, _LOGGER) -REQUIREMENTS = ['pydeconz==43'] +REQUIREMENTS = ['pydeconz==44'] CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ @@ -119,6 +119,13 @@ def async_add_remote(sensors): deconz.start() + device_registry = await \ + hass.helpers.device_registry.async_get_registry() + device_registry.async_get_or_create( + [['bridgeid', deconz.config.bridgeid]], 'Dresden Elektronik', + deconz.config.modelid, [['Ethernet', deconz.config.mac]], + name=deconz.config.name, sw_version=deconz.config.swversion) + async def async_configure(call): """Set attribute of device in deCONZ. diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 20160edf8066f3..b2e32c771e8236 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -199,3 +199,21 @@ def device_state_attributes(self): if self._light.type == 'LightGroup': attributes['all_on'] = self._light.all_on return attributes + + @property + def device(self): + """Description for device registry.""" + if (self._light.uniqueid is not None and + self._light.uniqueid.count(':') == 7): + serial = self._light.uniqueid.split('-', 1)[0] + else: + return None + dev = { + 'identifiers': [['serial', serial]], + 'manufacturer': self._light.manufacturer, + 'model': self._light.modelid, + 'connection': [['Zigbee', serial]], + 'name': self._light.name, + 'sw_version': self._light.swversion, + } + return dev diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 7c492fd496d263..6fbd89347b4350 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -134,6 +134,24 @@ def device_state_attributes(self): attr[ATTR_DAYLIGHT] = self._sensor.daylight return attr + @property + def device(self): + """""" + if (self._sensor.uniqueid is not None and + self._sensor.uniqueid.count(':') == 7): + serial = self._sensor.uniqueid.split('-', 1)[0] + else: + return None + dev = { + 'identifiers': [['serial', serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'connection': [['Zigbee', serial]], + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } + return dev + class DeconzBattery(Entity): """Battery class for when a device is only represented as an event.""" @@ -192,3 +210,21 @@ def device_state_attributes(self): ATTR_EVENT_ID: slugify(self._device.name), } return attr + + @property + def device(self): + """Description for device registry.""" + if (self._device.uniqueid is not None and + self._device.uniqueid.count(':') == 7): + serial = self._device.uniqueid.split('-', 1)[0] + else: + return None + dev = { + 'identifiers': [['serial', serial]], + 'manufacturer': self._device.manufacturer, + 'model': self._device.modelid, + 'connection': [['Zigbee', serial]], + 'name': self._device.name, + 'sw_version': self._device.swversion, + } + return dev diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index d5fb22e97c467f..0f4d2c549a0fca 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -79,6 +79,24 @@ def should_poll(self): """No polling needed.""" return False + @property + def device(self): + """Description for device registry.""" + if (self._switch.uniqueid is not None and + self._switch.uniqueid.count(':') == 7): + serial = self._switch.uniqueid.split('-', 1)[0] + else: + return None + dev = { + 'identifiers': [['serial', serial]], + 'manufacturer': self._switch.manufacturer, + 'model': self._switch.modelid, + 'connection': [['Zigbee', serial]], + 'name': self._switch.name, + 'sw_version': self._switch.swversion, + } + return dev + class DeconzPowerPlug(DeconzSwitch): """Representation of power plugs from deCONZ.""" diff --git a/requirements_all.txt b/requirements_all.txt index 3732cb237c21e3..75856fa69af780 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -804,7 +804,7 @@ pycsspeechtts==1.0.2 pydaikin==0.4 # homeassistant.components.deconz -pydeconz==43 +pydeconz==44 # homeassistant.components.zwave pydispatcher==2.0.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 167155d688f0df..6e1c031fb8b1f3 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -136,7 +136,7 @@ py-canary==0.5.0 pyblackbird==0.5 # homeassistant.components.deconz -pydeconz==43 +pydeconz==44 # homeassistant.components.zwave pydispatcher==2.0.5 From 82c0745724dbab149241a284e147db008d50f913 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 21 Aug 2018 23:37:56 +0200 Subject: [PATCH 02/14] Fix hound comment --- homeassistant/components/deconz/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index 63cb915c8d2b94..32bdafffe56f3f 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -120,7 +120,7 @@ def async_add_remote(sensors): deconz.start() device_registry = await \ - hass.helpers.device_registry.async_get_registry() + hass.helpers.device_registry.async_get_registry() device_registry.async_get_or_create( [['bridgeid', deconz.config.bridgeid]], 'Dresden Elektronik', deconz.config.modelid, [['Ethernet', deconz.config.mac]], From 38b51a1c2e0f78fc2c3f1a0e14b87c1dff92a6ab Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 16:43:39 +0200 Subject: [PATCH 03/14] Fix Balloobs comments --- homeassistant/components/binary_sensor/deconz.py | 8 +++++--- homeassistant/components/deconz/__init__.py | 6 ++++-- homeassistant/components/light/deconz.py | 8 +++++--- homeassistant/components/sensor/deconz.py | 14 ++++++++------ homeassistant/components/switch/deconz.py | 8 +++++--- homeassistant/helpers/device_registry.py | 15 ++++++++++----- homeassistant/helpers/entity_platform.py | 7 +++++-- 7 files changed, 42 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 6edb7c6a30ec42..12da9507cce7c4 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -10,6 +10,8 @@ DATA_DECONZ_ID, DATA_DECONZ_UNSUB) from homeassistant.const import ATTR_BATTERY_LEVEL from homeassistant.core import callback +from homeassistant.helpers.device_registry import ( + CONNECTION_ZIGBEE, IDENTIFIER_MAC) from homeassistant.helpers.dispatcher import async_dispatcher_connect DEPENDENCIES = ['deconz'] @@ -119,14 +121,14 @@ def device(self): """Description for device registry.""" if (self._sensor.uniqueid is not None and self._sensor.uniqueid.count(':') == 7): - serial = self._sensor.uniqueid.split('-', 1)[0] + mac = self._sensor.uniqueid.split('-', 1)[0] else: return None dev = { - 'identifiers': [['serial', serial]], + 'connection': [[CONNECTION_ZIGBEE, mac]], + 'identifiers': [[IDENTIFIER_MAC, mac]], 'manufacturer': self._sensor.manufacturer, 'model': self._sensor.modelid, - 'connection': [['Zigbee', serial]], 'name': self._sensor.name, 'sw_version': self._sensor.swversion, } diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index 32bdafffe56f3f..d44fad4afb6a0d 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -12,6 +12,7 @@ CONF_ID, CONF_PORT, EVENT_HOMEASSISTANT_STOP) from homeassistant.core import EventOrigin, callback from homeassistant.helpers import aiohttp_client, config_validation as cv +from homeassistant.helpers.device_registry import CONNECTION_MAC from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send) from homeassistant.util import slugify @@ -122,8 +123,9 @@ def async_add_remote(sensors): device_registry = await \ hass.helpers.device_registry.async_get_registry() device_registry.async_get_or_create( - [['bridgeid', deconz.config.bridgeid]], 'Dresden Elektronik', - deconz.config.modelid, [['Ethernet', deconz.config.mac]], + connection=[[CONNECTION_MAC, deconz.config.mac]], + identifiers=[[DOMAIN, deconz.config.bridgeid]], + manufacturer='Dresden Elektronik', model=deconz.config.modelid, name=deconz.config.name, sw_version=deconz.config.swversion) async def async_configure(call): diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index b2e32c771e8236..4347902cfc1e14 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -13,6 +13,8 @@ SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_TRANSITION, Light) from homeassistant.core import callback +from homeassistant.helpers.device_registry import ( + CONNECTION_ZIGBEE, IDENTIFIER_MAC) from homeassistant.helpers.dispatcher import async_dispatcher_connect import homeassistant.util.color as color_util @@ -205,14 +207,14 @@ def device(self): """Description for device registry.""" if (self._light.uniqueid is not None and self._light.uniqueid.count(':') == 7): - serial = self._light.uniqueid.split('-', 1)[0] + mac = self._light.uniqueid.split('-', 1)[0] else: return None dev = { - 'identifiers': [['serial', serial]], + 'connection': [[CONNECTION_ZIGBEE, mac]], + 'identifiers': [[IDENTIFIER_MAC, mac]], 'manufacturer': self._light.manufacturer, 'model': self._light.modelid, - 'connection': [['Zigbee', serial]], 'name': self._light.name, 'sw_version': self._light.swversion, } diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 6fbd89347b4350..ae61be7b064aaf 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -10,6 +10,8 @@ from homeassistant.const import ( ATTR_BATTERY_LEVEL, ATTR_VOLTAGE, DEVICE_CLASS_BATTERY) from homeassistant.core import callback +from homeassistant.helpers.device_registry import ( + CONNECTION_ZIGBEE, IDENTIFIER_MAC) from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from homeassistant.util import slugify @@ -139,14 +141,14 @@ def device(self): """""" if (self._sensor.uniqueid is not None and self._sensor.uniqueid.count(':') == 7): - serial = self._sensor.uniqueid.split('-', 1)[0] + mac = self._sensor.uniqueid.split('-', 1)[0] else: return None dev = { - 'identifiers': [['serial', serial]], + 'connection': [[CONNECTION_ZIGBEE, mac]], + 'identifiers': [[IDENTIFIER_MAC, mac]], 'manufacturer': self._sensor.manufacturer, 'model': self._sensor.modelid, - 'connection': [['Zigbee', serial]], 'name': self._sensor.name, 'sw_version': self._sensor.swversion, } @@ -216,14 +218,14 @@ def device(self): """Description for device registry.""" if (self._device.uniqueid is not None and self._device.uniqueid.count(':') == 7): - serial = self._device.uniqueid.split('-', 1)[0] + mac = self._device.uniqueid.split('-', 1)[0] else: return None dev = { - 'identifiers': [['serial', serial]], + 'connection': [[CONNECTION_ZIGBEE, mac]], + 'identifiers': [[IDENTIFIER_MAC, mac]], 'manufacturer': self._device.manufacturer, 'model': self._device.modelid, - 'connection': [['Zigbee', serial]], 'name': self._device.name, 'sw_version': self._device.swversion, } diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 0f4d2c549a0fca..efc68091f9bda2 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -9,6 +9,8 @@ POWER_PLUGS, SIRENS) from homeassistant.components.switch import SwitchDevice from homeassistant.core import callback +from homeassistant.helpers.device_registry import ( + CONNECTION_ZIGBEE, IDENTIFIER_MAC) from homeassistant.helpers.dispatcher import async_dispatcher_connect DEPENDENCIES = ['deconz'] @@ -84,14 +86,14 @@ def device(self): """Description for device registry.""" if (self._switch.uniqueid is not None and self._switch.uniqueid.count(':') == 7): - serial = self._switch.uniqueid.split('-', 1)[0] + mac = self._switch.uniqueid.split('-', 1)[0] else: return None dev = { - 'identifiers': [['serial', serial]], + 'connection': [[CONNECTION_ZIGBEE, mac]], + 'identifiers': [[IDENTIFIER_MAC, mac]], 'manufacturer': self._switch.manufacturer, 'model': self._switch.modelid, - 'connection': [['Zigbee', serial]], 'name': self._switch.name, 'sw_version': self._switch.swversion, } diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 78b38afa43861f..a9b4fb48256573 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -15,15 +15,20 @@ STORAGE_VERSION = 1 SAVE_DELAY = 10 +CONNECTION_MAC = 'mac' +CONNECTION_ZIGBEE = 'zigbee' + +IDENTIFIER_MAC = 'mac' + @attr.s(slots=True, frozen=True) class DeviceEntry: """Device Registry Entry.""" + connection = attr.ib(type=list) identifiers = attr.ib(type=list) manufacturer = attr.ib(type=str) model = attr.ib(type=str) - connection = attr.ib(type=list) name = attr.ib(type=str, default=None) sw_version = attr.ib(type=str, default=None) id = attr.ib(type=str, default=attr.Factory(lambda: uuid.uuid4().hex)) @@ -48,8 +53,8 @@ def async_get_device(self, identifiers: str, connections: tuple): return None @callback - def async_get_or_create(self, identifiers, manufacturer, model, - connection, *, name=None, sw_version=None): + def async_get_or_create(self, *, connection, identifiers, manufacturer, + model, name=None, sw_version=None): """Get device. Create if it doesn't exist.""" device = self.async_get_device(identifiers, connection) @@ -57,10 +62,10 @@ def async_get_or_create(self, identifiers, manufacturer, model, return device device = DeviceEntry( + connection=connection, identifiers=identifiers, manufacturer=manufacturer, model=model, - connection=connection, name=name, sw_version=sw_version ) @@ -93,10 +98,10 @@ def _data_to_save(self): data['devices'] = [ { 'id': entry.id, + 'connection': entry.connection, 'identifiers': entry.identifiers, 'manufacturer': entry.manufacturer, 'model': entry.model, - 'connection': entry.connection, 'name': entry.name, 'sw_version': entry.sw_version, } for entry in self.devices diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index fa880b014ec2b3..12d638fc7f1b1f 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -275,8 +275,11 @@ async def _async_add_entity(self, entity, update_before_add, device = entity.device if device is not None: device = device_registry.async_get_or_create( - device['identifiers'], device['manufacturer'], - device['model'], device['connection'], + connection=device['connection'], + identifiers=device['identifiers'], + manufacturer=device['manufacturer'], + model=device['model'], + name=device.get('name'), sw_version=device.get('sw_version')) device_id = device.id else: From bb8eb3807a393b949ded264cd617eca47ab5dd1b Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 16:49:17 +0200 Subject: [PATCH 04/14] Update tests --- tests/helpers/test_device_registry.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index 41e7d39e977a07..f7792eb52504b4 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -26,14 +26,17 @@ def registry(hass): async def test_get_or_create_returns_same_entry(registry): """Make sure we do not duplicate entries.""" entry = registry.async_get_or_create( - [['bridgeid', '0123']], 'manufacturer', 'model', - [['ethernet', '12:34:56:78:90:AB:CD:EF']]) + connection=[['ethernet', '12:34:56:78:90:AB:CD:EF']], + identifiers=[['bridgeid', '0123']], + manufacturer='manufacturer', model='model') entry2 = registry.async_get_or_create( - [['bridgeid', '0123']], 'manufacturer', 'model', - [['ethernet', '11:22:33:44:55:66:77:88']]) + connection=[['ethernet', '11:22:33:44:55:66:77:88']], + identifiers=[['bridgeid', '0123']], + manufacturer='manufacturer', model='model') entry3 = registry.async_get_or_create( - [['bridgeid', '1234']], 'manufacturer', 'model', - [['ethernet', '12:34:56:78:90:AB:CD:EF']]) + connection=[['ethernet', '12:34:56:78:90:AB:CD:EF']], + identifiers=[['bridgeid', '1234']], + manufacturer='manufacturer', model='model') assert len(registry.devices) == 1 assert entry is entry2 @@ -73,6 +76,7 @@ async def test_loading_from_storage(hass, hass_storage): registry = await device_registry.async_get_registry(hass) entry = registry.async_get_or_create( - [['serial', '12:34:56:78:90:AB:CD:EF']], 'manufacturer', - 'model', [['Zigbee', '01.23.45.67.89']]) + connection=[['Zigbee', '01.23.45.67.89']], + identifiers=[['serial', '12:34:56:78:90:AB:CD:EF']], + manufacturer='manufacturer', model='model') assert entry.id == 'abcdefghijklm' From 454c2da5f63de9d520a8d81c8716ad0977e3f1a1 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 16:57:27 +0200 Subject: [PATCH 05/14] Add missing pydoc string --- homeassistant/components/sensor/deconz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index ae61be7b064aaf..8ffa1932070bbf 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -138,7 +138,7 @@ def device_state_attributes(self): @property def device(self): - """""" + """Description for device registry.""" if (self._sensor.uniqueid is not None and self._sensor.uniqueid.count(':') == 7): mac = self._sensor.uniqueid.split('-', 1)[0] From 70699804266b2201efbf29fef4cba17180e9613c Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 18:44:48 +0200 Subject: [PATCH 06/14] Partial test fix --- tests/components/deconz/test_init.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index c6fc130a4a41a6..8e1c5cdb316016 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -94,6 +94,8 @@ async def test_setup_entry_successful(hass): with patch.object(hass, 'async_create_task') as mock_add_job, \ patch.object(hass, 'config_entries') as mock_config_entries, \ patch('pydeconz.DeconzSession.async_load_parameters', + return_value=mock_coro(True)), \ + patch('homeassistant.helpers.device_registry.async_get_registry', return_value=mock_coro(True)): assert await deconz.async_setup_entry(hass, entry) is True assert hass.data[deconz.DOMAIN] @@ -113,12 +115,14 @@ async def test_setup_entry_successful(hass): (entry, 'switch') -async def test_unload_entry(hass): +async def test_unload_entry(hass, aioclient_mock): """Test being able to unload an entry.""" entry = Mock() entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} with patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)): + return_value=mock_coro(True)), \ + patch('homeassistant.helpers.device_registry.async_get_registry', + return_value=mock_coro(True)): assert await deconz.async_setup_entry(hass, entry) is True assert deconz.DATA_DECONZ_EVENT in hass.data hass.data[deconz.DATA_DECONZ_EVENT].append(Mock()) From 15ba6ab37eb22ab30522d0a4032c262ce1ff6154 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 22:00:36 +0200 Subject: [PATCH 07/14] Update tests --- tests/components/deconz/test_init.py | 59 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index 8e1c5cdb316016..eaacbf6cdcd2a0 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -5,8 +5,17 @@ from homeassistant.setup import async_setup_component from homeassistant.components import deconz -from tests.common import mock_coro - +from tests.common import mock_coro, MockConfigEntry + +CONFIG = { + "config": { + "bridgeid": "0123456789ABCDEF", + "mac": "12:34:56:78:90:ab", + "modelid": "deCONZ", + "name": "Phoscon", + "swversion": "2.05.35" + } +} async def test_config_with_host_passed_to_config_entry(hass): """Test that configured options for a host are loaded via config entry.""" @@ -93,10 +102,11 @@ async def test_setup_entry_successful(hass): entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} with patch.object(hass, 'async_create_task') as mock_add_job, \ patch.object(hass, 'config_entries') as mock_config_entries, \ - patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)), \ + patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True), \ patch('homeassistant.helpers.device_registry.async_get_registry', - return_value=mock_coro(True)): + return_value=mock_coro(Mock())): assert await deconz.async_setup_entry(hass, entry) is True assert hass.data[deconz.DOMAIN] assert hass.data[deconz.DATA_DECONZ_ID] == {} @@ -115,14 +125,15 @@ async def test_setup_entry_successful(hass): (entry, 'switch') -async def test_unload_entry(hass, aioclient_mock): +async def test_unload_entry(hass): """Test being able to unload an entry.""" - entry = Mock() - entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} - with patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)), \ - patch('homeassistant.helpers.device_registry.async_get_registry', - return_value=mock_coro(True)): + entry = MockConfigEntry(domain=deconz.DOMAIN, data={ + 'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF' + }) + entry.add_to_hass(hass) + with patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True assert deconz.DATA_DECONZ_EVENT in hass.data hass.data[deconz.DATA_DECONZ_EVENT].append(Mock()) @@ -136,6 +147,9 @@ async def test_unload_entry(hass, aioclient_mock): async def test_add_new_device(hass): """Test adding a new device generates a signal for platforms.""" + entry = Mock() + entry.data = {'host': '1.2.3.4', 'port': 80, + 'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False} new_event = { "t": "event", "e": "added", @@ -151,11 +165,10 @@ async def test_add_new_device(hass): "type": "ZHASwitch" } } - entry = Mock() - entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} with patch.object(deconz, 'async_dispatcher_send') as mock_dispatch_send, \ - patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)): + patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True hass.data[deconz.DOMAIN].async_event_handler(new_event) await hass.async_block_till_done() @@ -166,15 +179,16 @@ async def test_add_new_device(hass): async def test_add_new_remote(hass): """Test new added device creates a new remote.""" entry = Mock() - entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} + entry.data = {'host': '1.2.3.4', 'port': 80, + 'api_key': '1234567890ABCDEF', 'allow_clip_sensor': False} remote = Mock() remote.name = 'name' remote.type = 'ZHASwitch' remote.register_async_callback = Mock() - with patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)): + with patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True - async_dispatcher_send(hass, 'deconz_new_sensor', [remote]) await hass.async_block_till_done() assert len(hass.data[deconz.DATA_DECONZ_EVENT]) == 1 @@ -189,8 +203,9 @@ async def test_do_not_allow_clip_sensor(hass): remote.name = 'name' remote.type = 'CLIPSwitch' remote.register_async_callback = Mock() - with patch('pydeconz.DeconzSession.async_load_parameters', - return_value=mock_coro(True)): + with patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True async_dispatcher_send(hass, 'deconz_new_sensor', [remote]) From 16a71913fa6de03c96da7b53535ff50370d3d5f5 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 22 Aug 2018 22:14:16 +0200 Subject: [PATCH 08/14] Fix styling comments --- tests/components/deconz/test_init.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index eaacbf6cdcd2a0..7f93eb41bdddf7 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -17,6 +17,7 @@ } } + async def test_config_with_host_passed_to_config_entry(hass): """Test that configured options for a host are loaded via config entry.""" with patch.object(hass, 'config_entries') as mock_config_entries, \ @@ -133,7 +134,7 @@ async def test_unload_entry(hass): entry.add_to_hass(hass) with patch('pydeconz.DeconzSession.async_get_state', return_value=mock_coro(CONFIG)), \ - patch('pydeconz.DeconzSession.start', return_value=True): + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True assert deconz.DATA_DECONZ_EVENT in hass.data hass.data[deconz.DATA_DECONZ_EVENT].append(Mock()) @@ -166,9 +167,9 @@ async def test_add_new_device(hass): } } with patch.object(deconz, 'async_dispatcher_send') as mock_dispatch_send, \ - patch('pydeconz.DeconzSession.async_get_state', - return_value=mock_coro(CONFIG)), \ - patch('pydeconz.DeconzSession.start', return_value=True): + patch('pydeconz.DeconzSession.async_get_state', + return_value=mock_coro(CONFIG)), \ + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True hass.data[deconz.DOMAIN].async_event_handler(new_event) await hass.async_block_till_done() @@ -205,7 +206,7 @@ async def test_do_not_allow_clip_sensor(hass): remote.register_async_callback = Mock() with patch('pydeconz.DeconzSession.async_get_state', return_value=mock_coro(CONFIG)), \ - patch('pydeconz.DeconzSession.start', return_value=True): + patch('pydeconz.DeconzSession.start', return_value=True): assert await deconz.async_setup_entry(hass, entry) is True async_dispatcher_send(hass, 'deconz_new_sensor', [remote]) From 7fcfc74474b297dc63a5c75b0ddf9843bf386335 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 23 Aug 2018 16:48:10 +0200 Subject: [PATCH 09/14] Fix test --- tests/components/deconz/test_init.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index 7f93eb41bdddf7..049a3b961b6109 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -5,7 +5,7 @@ from homeassistant.setup import async_setup_component from homeassistant.components import deconz -from tests.common import mock_coro, MockConfigEntry +from tests.common import mock_coro CONFIG = { "config": { @@ -128,15 +128,17 @@ async def test_setup_entry_successful(hass): async def test_unload_entry(hass): """Test being able to unload an entry.""" - entry = MockConfigEntry(domain=deconz.DOMAIN, data={ - 'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF' - }) - entry.add_to_hass(hass) - with patch('pydeconz.DeconzSession.async_get_state', - return_value=mock_coro(CONFIG)), \ - patch('pydeconz.DeconzSession.start', return_value=True): + entry = Mock() + entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'} + entry.async_unload.return_value = mock_coro(True) + deconzmock = Mock() + deconzmock.async_load_parameters.return_value = mock_coro(True) + deconzmock.sensors = {} + with patch('pydeconz.DeconzSession', return_value=deconzmock): assert await deconz.async_setup_entry(hass, entry) is True + assert deconz.DATA_DECONZ_EVENT in hass.data + hass.data[deconz.DATA_DECONZ_EVENT].append(Mock()) hass.data[deconz.DATA_DECONZ_ID] = {'id': 'deconzid'} assert await deconz.async_unload_entry(hass, entry) From 7b48826a1862986273f7a109d937f27e3eaa133b Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 23 Aug 2018 17:00:24 +0200 Subject: [PATCH 10/14] Improve readability of platform device descriptions --- .../components/binary_sensor/deconz.py | 22 +++++----- homeassistant/components/light/deconz.py | 22 +++++----- homeassistant/components/sensor/deconz.py | 44 +++++++++---------- homeassistant/components/switch/deconz.py | 22 +++++----- 4 files changed, 50 insertions(+), 60 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 12da9507cce7c4..8f3fa29cadff0e 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -121,15 +121,13 @@ def device(self): """Description for device registry.""" if (self._sensor.uniqueid is not None and self._sensor.uniqueid.count(':') == 7): - mac = self._sensor.uniqueid.split('-', 1)[0] - else: - return None - dev = { - 'connection': [[CONNECTION_ZIGBEE, mac]], - 'identifiers': [[IDENTIFIER_MAC, mac]], - 'manufacturer': self._sensor.manufacturer, - 'model': self._sensor.modelid, - 'name': self._sensor.name, - 'sw_version': self._sensor.swversion, - } - return dev + serial = self._sensor.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[IDENTIFIER_MAC, serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } + return None diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 4347902cfc1e14..1801fd4fbb140d 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -207,15 +207,13 @@ def device(self): """Description for device registry.""" if (self._light.uniqueid is not None and self._light.uniqueid.count(':') == 7): - mac = self._light.uniqueid.split('-', 1)[0] - else: - return None - dev = { - 'connection': [[CONNECTION_ZIGBEE, mac]], - 'identifiers': [[IDENTIFIER_MAC, mac]], - 'manufacturer': self._light.manufacturer, - 'model': self._light.modelid, - 'name': self._light.name, - 'sw_version': self._light.swversion, - } - return dev + serial = self._light.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[IDENTIFIER_MAC, serial]], + 'manufacturer': self._light.manufacturer, + 'model': self._light.modelid, + 'name': self._light.name, + 'sw_version': self._light.swversion, + } + return None diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 8ffa1932070bbf..ff4eb36d4d2685 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -141,18 +141,16 @@ def device(self): """Description for device registry.""" if (self._sensor.uniqueid is not None and self._sensor.uniqueid.count(':') == 7): - mac = self._sensor.uniqueid.split('-', 1)[0] - else: - return None - dev = { - 'connection': [[CONNECTION_ZIGBEE, mac]], - 'identifiers': [[IDENTIFIER_MAC, mac]], - 'manufacturer': self._sensor.manufacturer, - 'model': self._sensor.modelid, - 'name': self._sensor.name, - 'sw_version': self._sensor.swversion, - } - return dev + serial = self._sensor.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[IDENTIFIER_MAC, serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } + return None class DeconzBattery(Entity): @@ -218,15 +216,13 @@ def device(self): """Description for device registry.""" if (self._device.uniqueid is not None and self._device.uniqueid.count(':') == 7): - mac = self._device.uniqueid.split('-', 1)[0] - else: - return None - dev = { - 'connection': [[CONNECTION_ZIGBEE, mac]], - 'identifiers': [[IDENTIFIER_MAC, mac]], - 'manufacturer': self._device.manufacturer, - 'model': self._device.modelid, - 'name': self._device.name, - 'sw_version': self._device.swversion, - } - return dev + serial = self._device.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[IDENTIFIER_MAC, serial]], + 'manufacturer': self._device.manufacturer, + 'model': self._device.modelid, + 'name': self._device.name, + 'sw_version': self._device.swversion, + } + return None diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index efc68091f9bda2..3378dcf185539c 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -86,18 +86,16 @@ def device(self): """Description for device registry.""" if (self._switch.uniqueid is not None and self._switch.uniqueid.count(':') == 7): - mac = self._switch.uniqueid.split('-', 1)[0] - else: - return None - dev = { - 'connection': [[CONNECTION_ZIGBEE, mac]], - 'identifiers': [[IDENTIFIER_MAC, mac]], - 'manufacturer': self._switch.manufacturer, - 'model': self._switch.modelid, - 'name': self._switch.name, - 'sw_version': self._switch.swversion, - } - return dev + serial = self._switch.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[IDENTIFIER_MAC, serial]], + 'manufacturer': self._switch.manufacturer, + 'model': self._switch.modelid, + 'name': self._switch.name, + 'sw_version': self._switch.swversion, + } + return None class DeconzPowerPlug(DeconzSwitch): From 4d3c28c2b5867f3cd82eda9826c64599b222bd40 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 23 Aug 2018 17:50:03 +0200 Subject: [PATCH 11/14] Fix identifiers --- homeassistant/components/binary_sensor/deconz.py | 7 +++---- homeassistant/components/deconz/__init__.py | 4 ++-- homeassistant/components/deconz/const.py | 1 + homeassistant/components/light/deconz.py | 7 +++---- homeassistant/components/sensor/deconz.py | 9 ++++----- homeassistant/components/switch/deconz.py | 7 +++---- homeassistant/helpers/device_registry.py | 4 +--- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 8f3fa29cadff0e..b83fe092aec98a 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -7,11 +7,10 @@ from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.deconz.const import ( ATTR_DARK, ATTR_ON, CONF_ALLOW_CLIP_SENSOR, DOMAIN as DATA_DECONZ, - DATA_DECONZ_ID, DATA_DECONZ_UNSUB) + DATA_DECONZ_ID, DATA_DECONZ_UNSUB, DECONZ_DOMAIN) from homeassistant.const import ATTR_BATTERY_LEVEL from homeassistant.core import callback -from homeassistant.helpers.device_registry import ( - CONNECTION_ZIGBEE, IDENTIFIER_MAC) +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from homeassistant.helpers.dispatcher import async_dispatcher_connect DEPENDENCIES = ['deconz'] @@ -124,7 +123,7 @@ def device(self): serial = self._sensor.uniqueid.split('-', 1)[0] return { 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[IDENTIFIER_MAC, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], 'manufacturer': self._sensor.manufacturer, 'model': self._sensor.modelid, 'name': self._sensor.name, diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index d44fad4afb6a0d..e9650beb409151 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -12,7 +12,7 @@ CONF_ID, CONF_PORT, EVENT_HOMEASSISTANT_STOP) from homeassistant.core import EventOrigin, callback from homeassistant.helpers import aiohttp_client, config_validation as cv -from homeassistant.helpers.device_registry import CONNECTION_MAC +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send) from homeassistant.util import slugify @@ -123,7 +123,7 @@ def async_add_remote(sensors): device_registry = await \ hass.helpers.device_registry.async_get_registry() device_registry.async_get_or_create( - connection=[[CONNECTION_MAC, deconz.config.mac]], + connection=[[CONNECTION_NETWORK_MAC, deconz.config.mac]], identifiers=[[DOMAIN, deconz.config.bridgeid]], manufacturer='Dresden Elektronik', model=deconz.config.modelid, name=deconz.config.name, sw_version=deconz.config.swversion) diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py index e7bc5605aee400..e629d57f2017f4 100644 --- a/homeassistant/components/deconz/const.py +++ b/homeassistant/components/deconz/const.py @@ -8,6 +8,7 @@ DATA_DECONZ_EVENT = 'deconz_events' DATA_DECONZ_ID = 'deconz_entities' DATA_DECONZ_UNSUB = 'deconz_dispatchers' +DECONZ_DOMAIN = 'deconz' CONF_ALLOW_CLIP_SENSOR = 'allow_clip_sensor' CONF_ALLOW_DECONZ_GROUPS = 'allow_deconz_groups' diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 1801fd4fbb140d..5893721125246a 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -6,15 +6,14 @@ """ from homeassistant.components.deconz.const import ( CONF_ALLOW_DECONZ_GROUPS, DOMAIN as DATA_DECONZ, - DATA_DECONZ_ID, DATA_DECONZ_UNSUB, SWITCH_TYPES) + DATA_DECONZ_ID, DATA_DECONZ_UNSUB, DECONZ_DOMAIN, SWITCH_TYPES) from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR, ATTR_TRANSITION, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_TRANSITION, Light) from homeassistant.core import callback -from homeassistant.helpers.device_registry import ( - CONNECTION_ZIGBEE, IDENTIFIER_MAC) +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from homeassistant.helpers.dispatcher import async_dispatcher_connect import homeassistant.util.color as color_util @@ -210,7 +209,7 @@ def device(self): serial = self._light.uniqueid.split('-', 1)[0] return { 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[IDENTIFIER_MAC, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], 'manufacturer': self._light.manufacturer, 'model': self._light.modelid, 'name': self._light.name, diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index ff4eb36d4d2685..20c0463c2738d4 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -6,12 +6,11 @@ """ from homeassistant.components.deconz.const import ( ATTR_DARK, ATTR_ON, CONF_ALLOW_CLIP_SENSOR, DOMAIN as DATA_DECONZ, - DATA_DECONZ_ID, DATA_DECONZ_UNSUB) + DATA_DECONZ_ID, DATA_DECONZ_UNSUB, DECONZ_DOMAIN) from homeassistant.const import ( ATTR_BATTERY_LEVEL, ATTR_VOLTAGE, DEVICE_CLASS_BATTERY) from homeassistant.core import callback -from homeassistant.helpers.device_registry import ( - CONNECTION_ZIGBEE, IDENTIFIER_MAC) +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from homeassistant.util import slugify @@ -144,7 +143,7 @@ def device(self): serial = self._sensor.uniqueid.split('-', 1)[0] return { 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[IDENTIFIER_MAC, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], 'manufacturer': self._sensor.manufacturer, 'model': self._sensor.modelid, 'name': self._sensor.name, @@ -219,7 +218,7 @@ def device(self): serial = self._device.uniqueid.split('-', 1)[0] return { 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[IDENTIFIER_MAC, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], 'manufacturer': self._device.manufacturer, 'model': self._device.modelid, 'name': self._device.name, diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 3378dcf185539c..55b7c3c574f80d 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -6,11 +6,10 @@ """ from homeassistant.components.deconz.const import ( DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB, - POWER_PLUGS, SIRENS) + DECONZ_DOMAIN, POWER_PLUGS, SIRENS) from homeassistant.components.switch import SwitchDevice from homeassistant.core import callback -from homeassistant.helpers.device_registry import ( - CONNECTION_ZIGBEE, IDENTIFIER_MAC) +from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE from homeassistant.helpers.dispatcher import async_dispatcher_connect DEPENDENCIES = ['deconz'] @@ -89,7 +88,7 @@ def device(self): serial = self._switch.uniqueid.split('-', 1)[0] return { 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[IDENTIFIER_MAC, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], 'manufacturer': self._switch.manufacturer, 'model': self._switch.modelid, 'name': self._switch.name, diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index a9b4fb48256573..4becacc9e2fed2 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -15,11 +15,9 @@ STORAGE_VERSION = 1 SAVE_DELAY = 10 -CONNECTION_MAC = 'mac' +CONNECTION_NETWORK_MAC = 'mac' CONNECTION_ZIGBEE = 'zigbee' -IDENTIFIER_MAC = 'mac' - @attr.s(slots=True, frozen=True) class DeviceEntry: From df79e77e88269ee5459d01b54ae71e3a2389e021 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 24 Aug 2018 16:45:19 +0200 Subject: [PATCH 12/14] Inverse check in device property --- .../components/binary_sensor/deconz.py | 22 +++++----- homeassistant/components/light/deconz.py | 22 +++++----- homeassistant/components/sensor/deconz.py | 44 +++++++++---------- homeassistant/components/switch/deconz.py | 22 +++++----- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index b83fe092aec98a..5c83191e0bc3d8 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -119,14 +119,14 @@ def device_state_attributes(self): def device(self): """Description for device registry.""" if (self._sensor.uniqueid is not None and - self._sensor.uniqueid.count(':') == 7): - serial = self._sensor.uniqueid.split('-', 1)[0] - return { - 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[DECONZ_DOMAIN, serial]], - 'manufacturer': self._sensor.manufacturer, - 'model': self._sensor.modelid, - 'name': self._sensor.name, - 'sw_version': self._sensor.swversion, - } - return None + self._sensor.uniqueid.count(':') != 7): + return None + serial = self._sensor.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 5893721125246a..2065af22f03b79 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -205,14 +205,14 @@ def device_state_attributes(self): def device(self): """Description for device registry.""" if (self._light.uniqueid is not None and - self._light.uniqueid.count(':') == 7): - serial = self._light.uniqueid.split('-', 1)[0] - return { - 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[DECONZ_DOMAIN, serial]], - 'manufacturer': self._light.manufacturer, - 'model': self._light.modelid, - 'name': self._light.name, - 'sw_version': self._light.swversion, - } - return None + self._light.uniqueid.count(':') != 7): + return None + serial = self._light.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], + 'manufacturer': self._light.manufacturer, + 'model': self._light.modelid, + 'name': self._light.name, + 'sw_version': self._light.swversion, + } diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 20c0463c2738d4..380d299e835f42 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -139,17 +139,17 @@ def device_state_attributes(self): def device(self): """Description for device registry.""" if (self._sensor.uniqueid is not None and - self._sensor.uniqueid.count(':') == 7): - serial = self._sensor.uniqueid.split('-', 1)[0] - return { - 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[DECONZ_DOMAIN, serial]], - 'manufacturer': self._sensor.manufacturer, - 'model': self._sensor.modelid, - 'name': self._sensor.name, - 'sw_version': self._sensor.swversion, - } - return None + self._sensor.uniqueid.count(':') != 7): + return None + serial = self._sensor.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], + 'manufacturer': self._sensor.manufacturer, + 'model': self._sensor.modelid, + 'name': self._sensor.name, + 'sw_version': self._sensor.swversion, + } class DeconzBattery(Entity): @@ -214,14 +214,14 @@ def device_state_attributes(self): def device(self): """Description for device registry.""" if (self._device.uniqueid is not None and - self._device.uniqueid.count(':') == 7): - serial = self._device.uniqueid.split('-', 1)[0] - return { - 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[DECONZ_DOMAIN, serial]], - 'manufacturer': self._device.manufacturer, - 'model': self._device.modelid, - 'name': self._device.name, - 'sw_version': self._device.swversion, - } - return None + self._device.uniqueid.count(':') != 7): + return None + serial = self._device.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], + 'manufacturer': self._device.manufacturer, + 'model': self._device.modelid, + 'name': self._device.name, + 'sw_version': self._device.swversion, + } diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 55b7c3c574f80d..eaed85b6870323 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -84,17 +84,17 @@ def should_poll(self): def device(self): """Description for device registry.""" if (self._switch.uniqueid is not None and - self._switch.uniqueid.count(':') == 7): - serial = self._switch.uniqueid.split('-', 1)[0] - return { - 'connection': [[CONNECTION_ZIGBEE, serial]], - 'identifiers': [[DECONZ_DOMAIN, serial]], - 'manufacturer': self._switch.manufacturer, - 'model': self._switch.modelid, - 'name': self._switch.name, - 'sw_version': self._switch.swversion, - } - return None + self._switch.uniqueid.count(':') != 7): + return None + serial = self._switch.uniqueid.split('-', 1)[0] + return { + 'connection': [[CONNECTION_ZIGBEE, serial]], + 'identifiers': [[DECONZ_DOMAIN, serial]], + 'manufacturer': self._switch.manufacturer, + 'model': self._switch.modelid, + 'name': self._switch.name, + 'sw_version': self._switch.swversion, + } class DeconzPowerPlug(DeconzSwitch): From 33405d88e77fb51046b98cfdfde2d105a7efe43d Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 24 Aug 2018 16:51:55 +0200 Subject: [PATCH 13/14] Added second half of logic --- homeassistant/components/binary_sensor/deconz.py | 2 +- homeassistant/components/light/deconz.py | 2 +- homeassistant/components/sensor/deconz.py | 4 ++-- homeassistant/components/switch/deconz.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 5c83191e0bc3d8..16b67a5b68013d 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -118,7 +118,7 @@ def device_state_attributes(self): @property def device(self): """Description for device registry.""" - if (self._sensor.uniqueid is not None and + if (self._sensor.uniqueid is None or self._sensor.uniqueid.count(':') != 7): return None serial = self._sensor.uniqueid.split('-', 1)[0] diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 2065af22f03b79..46aaa0433f6f9e 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -204,7 +204,7 @@ def device_state_attributes(self): @property def device(self): """Description for device registry.""" - if (self._light.uniqueid is not None and + if (self._light.uniqueid is None or self._light.uniqueid.count(':') != 7): return None serial = self._light.uniqueid.split('-', 1)[0] diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 380d299e835f42..920ce0831efb5a 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -138,7 +138,7 @@ def device_state_attributes(self): @property def device(self): """Description for device registry.""" - if (self._sensor.uniqueid is not None and + if (self._sensor.uniqueid is None or self._sensor.uniqueid.count(':') != 7): return None serial = self._sensor.uniqueid.split('-', 1)[0] @@ -213,7 +213,7 @@ def device_state_attributes(self): @property def device(self): """Description for device registry.""" - if (self._device.uniqueid is not None and + if (self._device.uniqueid is None or self._device.uniqueid.count(':') != 7): return None serial = self._device.uniqueid.split('-', 1)[0] diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index eaed85b6870323..02106803f8d824 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -83,7 +83,7 @@ def should_poll(self): @property def device(self): """Description for device registry.""" - if (self._switch.uniqueid is not None and + if (self._switch.uniqueid is None or self._switch.uniqueid.count(':') != 7): return None serial = self._switch.uniqueid.split('-', 1)[0] From 067c0f87b6b67448171ea4ed8b2d4d24a4b57158 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Fri, 24 Aug 2018 17:20:17 +0200 Subject: [PATCH 14/14] Lint --- homeassistant/components/binary_sensor/deconz.py | 2 +- homeassistant/components/light/deconz.py | 2 +- homeassistant/components/sensor/deconz.py | 4 ++-- homeassistant/components/switch/deconz.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/deconz.py b/homeassistant/components/binary_sensor/deconz.py index 16b67a5b68013d..c92c84c9d42c28 100644 --- a/homeassistant/components/binary_sensor/deconz.py +++ b/homeassistant/components/binary_sensor/deconz.py @@ -117,7 +117,7 @@ def device_state_attributes(self): @property def device(self): - """Description for device registry.""" + """Return a device description for device registry.""" if (self._sensor.uniqueid is None or self._sensor.uniqueid.count(':') != 7): return None diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py index 46aaa0433f6f9e..a9980e5528a908 100644 --- a/homeassistant/components/light/deconz.py +++ b/homeassistant/components/light/deconz.py @@ -203,7 +203,7 @@ def device_state_attributes(self): @property def device(self): - """Description for device registry.""" + """Return a device description for device registry.""" if (self._light.uniqueid is None or self._light.uniqueid.count(':') != 7): return None diff --git a/homeassistant/components/sensor/deconz.py b/homeassistant/components/sensor/deconz.py index 920ce0831efb5a..2b6c12e37b20d0 100644 --- a/homeassistant/components/sensor/deconz.py +++ b/homeassistant/components/sensor/deconz.py @@ -137,7 +137,7 @@ def device_state_attributes(self): @property def device(self): - """Description for device registry.""" + """Return a device description for device registry.""" if (self._sensor.uniqueid is None or self._sensor.uniqueid.count(':') != 7): return None @@ -212,7 +212,7 @@ def device_state_attributes(self): @property def device(self): - """Description for device registry.""" + """Return a device description for device registry.""" if (self._device.uniqueid is None or self._device.uniqueid.count(':') != 7): return None diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 02106803f8d824..57201a9e2219ad 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -82,7 +82,7 @@ def should_poll(self): @property def device(self): - """Description for device registry.""" + """Return a device description for device registry.""" if (self._switch.uniqueid is None or self._switch.uniqueid.count(':') != 7): return None