Add device_info to enable HA-devices for Homematic IP (#21241)

* add device_info to device

* added checks

* Fixes based on feedback

* Fix spelling

* Simplified implementation

On homematicip devices and the ap are created

* small fix with device.id

* hub/ap device creation moved to __init__.py

* Fixed result handling

* fixes after review.

* Fix test
This commit is contained in:
Markus Jankowski 2019-02-27 20:25:11 +01:00 committed by Paulus Schoutsen
parent 9066609d23
commit 9b3a3fc1ac
4 changed files with 51 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.helpers import device_registry as dr
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from .config_flow import configured_haps from .config_flow import configured_haps
@ -52,7 +53,22 @@ async def async_setup_entry(hass, entry):
hap = HomematicipHAP(hass, entry) hap = HomematicipHAP(hass, entry)
hapid = entry.data[HMIPC_HAPID].replace('-', '').upper() hapid = entry.data[HMIPC_HAPID].replace('-', '').upper()
hass.data[DOMAIN][hapid] = hap hass.data[DOMAIN][hapid] = hap
return await hap.async_setup()
if not await hap.async_setup():
return False
# Register hap as device in registry.
device_registry = await dr.async_get_registry(hass)
home = hap.home
device_registry.async_get_or_create(
config_entry_id=home.id,
identifiers={(DOMAIN, home.id)},
manufacturer='eQ-3',
name=home.label,
model=home.modelType,
sw_version=home.currentAPVersion,
)
return True
async def async_unload_entry(hass, entry): async def async_unload_entry(hass, entry):

View File

@ -1,6 +1,7 @@
"""Generic device for the HomematicIP Cloud component.""" """Generic device for the HomematicIP Cloud component."""
import logging import logging
from homeassistant.components import homematicip_cloud
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -32,6 +33,25 @@ class HomematicipGenericDevice(Entity):
self.post = post self.post = post
_LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType) _LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType)
@property
def device_info(self):
"""Return device specific attributes."""
from homematicip.aio.device import AsyncDevice
# Only physical devices should be HA devices.
if isinstance(self._device, AsyncDevice):
return {
'identifiers': {
# Serial numbers of Homematic IP device
(homematicip_cloud.DOMAIN, self._device.id)
},
'name': self._device.label,
'manufacturer': self._device.oem,
'model': self._device.modelType,
'sw_version': self._device.firmwareVersion,
'via_hub': (homematicip_cloud.DOMAIN, self._device.homeId),
}
return None
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
self._device.on_update(self._device_changed) self._device.on_update(self._device_changed)

View File

@ -65,6 +65,17 @@ class HomematicipAccesspointStatus(HomematicipGenericDevice):
"""Initialize access point device.""" """Initialize access point device."""
super().__init__(home, home) super().__init__(home, home)
@property
def device_info(self):
"""Return device specific attributes."""
# Adds a sensor to the existing HAP device
return {
'identifiers': {
# Serial numbers of Homematic IP device
(HMIPC_DOMAIN, self._device.id)
}
}
@property @property
def icon(self): def icon(self):
"""Return the icon of the access point device.""" """Return the icon of the access point device."""

View File

@ -21,7 +21,7 @@ async def test_config_with_accesspoint_passed_to_config_entry(hass):
}) is True }) is True
# Flow started for the access point # Flow started for the access point
assert len(mock_config_entries.flow.mock_calls) == 2 assert len(mock_config_entries.flow.mock_calls) >= 2
async def test_config_already_registered_not_passed_to_config_entry(hass): async def test_config_already_registered_not_passed_to_config_entry(hass):
@ -58,7 +58,7 @@ async def test_setup_entry_successful(hass):
} }
}) is True }) is True
assert len(mock_hap.mock_calls) == 2 assert len(mock_hap.mock_calls) >= 2
async def test_setup_defined_accesspoint(hass): async def test_setup_defined_accesspoint(hass):
@ -95,7 +95,7 @@ async def test_unload_entry(hass):
mock_hap.return_value.async_setup.return_value = mock_coro(True) mock_hap.return_value.async_setup.return_value = mock_coro(True)
assert await async_setup_component(hass, hmipc.DOMAIN, {}) is True assert await async_setup_component(hass, hmipc.DOMAIN, {}) is True
assert len(mock_hap.return_value.mock_calls) == 1 assert len(mock_hap.return_value.mock_calls) >= 1
mock_hap.return_value.async_reset.return_value = mock_coro(True) mock_hap.return_value.async_reset.return_value = mock_coro(True)
assert await hmipc.async_unload_entry(hass, entry) assert await hmipc.async_unload_entry(hass, entry)