Add create, remove of devices for HomematicIP_Cloud (#27030)

This commit is contained in:
SukramJ 2019-09-28 22:34:14 +02:00 committed by Martin Hjelmare
parent 11c9bab078
commit 61a7d8e3d2
5 changed files with 64 additions and 3 deletions

View File

@ -6,6 +6,8 @@ from homematicip.aio.device import AsyncDevice
from homematicip.aio.home import AsyncHome
from homeassistant.components import homematicip_cloud
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
@ -51,6 +53,8 @@ class HomematicipGenericDevice(Entity):
self._home = home
self._device = device
self.post = post
# Marker showing that the HmIP device hase been removed.
self.hmip_device_removed = False
_LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType)
@property
@ -74,7 +78,9 @@ class HomematicipGenericDevice(Entity):
async def async_added_to_hass(self):
"""Register callbacks."""
self._device.on_update(self._async_device_changed)
self._device.on_remove(self._async_device_removed)
@callback
def _async_device_changed(self, *args, **kwargs):
"""Handle device state changes."""
# Don't update disabled entities
@ -88,6 +94,47 @@ class HomematicipGenericDevice(Entity):
self._device.modelType,
)
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
# Only go further if the device/entity should be removed from registries
# due to a removal of the HmIP device.
if self.hmip_device_removed:
await self.async_remove_from_registries()
async def async_remove_from_registries(self) -> None:
"""Remove entity/device from registry."""
# Remove callback from device.
self._device.remove_callback(self._async_device_changed)
self._device.remove_callback(self._async_device_removed)
if not self.registry_entry:
return
device_id = self.registry_entry.device_id
if device_id:
# Remove from device registry.
device_registry = await dr.async_get_registry(self.hass)
if device_id in device_registry.devices:
# This will also remove associated entities from entity registry.
device_registry.async_remove_device(device_id)
else:
# Remove from entity registry.
# Only relevant for entities that do not belong to a device.
entity_id = self.registry_entry.entity_id
if entity_id:
entity_registry = await er.async_get_registry(self.hass)
if entity_id in entity_registry.entities:
entity_registry.async_remove(entity_id)
@callback
def _async_device_removed(self, *args, **kwargs):
"""Handle hmip device removal."""
# Set marker showing that the HmIP device hase been removed.
self.hmip_device_removed = True
self.hass.async_create_task(self.async_remove())
@property
def name(self) -> str:
"""Return the name of the generic device."""

View File

@ -5,6 +5,7 @@ import logging
from homematicip.aio.auth import AsyncAuth
from homematicip.aio.home import AsyncHome
from homematicip.base.base_connection import HmipConnectionError
from homematicip.base.enums import EventType
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -137,6 +138,18 @@ class HomematicipHAP:
self.home.update_home_only(args[0])
@callback
def async_create_entity(self, *args, **kwargs):
"""Create a device or a group."""
is_device = EventType(kwargs["event_type"]) == EventType.DEVICE_ADDED
self.hass.async_create_task(self.async_create_entity_lazy(is_device))
async def async_create_entity_lazy(self, is_device=True):
"""Delay entity creation to allow the user to enter a device name."""
if is_device:
await asyncio.sleep(30)
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
async def get_state(self):
"""Update HMIP state and tell Home Assistant."""
await self.home.get_current_state()
@ -225,6 +238,7 @@ class HomematicipHAP:
except HmipConnectionError:
raise HmipcConnectionError
home.on_update(self.async_update)
home.on_create(self.async_create_entity)
hass.loop.create_task(self.async_connect())
return home

View File

@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/components/homematicip_cloud",
"requirements": [
"homematicip==0.10.11"
"homematicip==0.10.12"
],
"dependencies": [],
"codeowners": [

View File

@ -649,7 +649,7 @@ homeassistant-pyozw==0.1.4
homekit[IP]==0.15.0
# homeassistant.components.homematicip_cloud
homematicip==0.10.11
homematicip==0.10.12
# homeassistant.components.horizon
horimote==0.4.1

View File

@ -188,7 +188,7 @@ home-assistant-frontend==20190919.1
homekit[IP]==0.15.0
# homeassistant.components.homematicip_cloud
homematicip==0.10.11
homematicip==0.10.12
# homeassistant.components.google
# homeassistant.components.remember_the_milk