mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Add create, remove of devices for HomematicIP_Cloud (#27030)
This commit is contained in:
parent
11c9bab078
commit
61a7d8e3d2
@ -6,6 +6,8 @@ from homematicip.aio.device import AsyncDevice
|
|||||||
from homematicip.aio.home import AsyncHome
|
from homematicip.aio.home import AsyncHome
|
||||||
|
|
||||||
from homeassistant.components import homematicip_cloud
|
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
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -51,6 +53,8 @@ class HomematicipGenericDevice(Entity):
|
|||||||
self._home = home
|
self._home = home
|
||||||
self._device = device
|
self._device = device
|
||||||
self.post = post
|
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)
|
_LOGGER.info("Setting up %s (%s)", self.name, self._device.modelType)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -74,7 +78,9 @@ class HomematicipGenericDevice(Entity):
|
|||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
self._device.on_update(self._async_device_changed)
|
self._device.on_update(self._async_device_changed)
|
||||||
|
self._device.on_remove(self._async_device_removed)
|
||||||
|
|
||||||
|
@callback
|
||||||
def _async_device_changed(self, *args, **kwargs):
|
def _async_device_changed(self, *args, **kwargs):
|
||||||
"""Handle device state changes."""
|
"""Handle device state changes."""
|
||||||
# Don't update disabled entities
|
# Don't update disabled entities
|
||||||
@ -88,6 +94,47 @@ class HomematicipGenericDevice(Entity):
|
|||||||
self._device.modelType,
|
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
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the generic device."""
|
"""Return the name of the generic device."""
|
||||||
|
@ -5,6 +5,7 @@ import logging
|
|||||||
from homematicip.aio.auth import AsyncAuth
|
from homematicip.aio.auth import AsyncAuth
|
||||||
from homematicip.aio.home import AsyncHome
|
from homematicip.aio.home import AsyncHome
|
||||||
from homematicip.base.base_connection import HmipConnectionError
|
from homematicip.base.base_connection import HmipConnectionError
|
||||||
|
from homematicip.base.enums import EventType
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@ -137,6 +138,18 @@ class HomematicipHAP:
|
|||||||
|
|
||||||
self.home.update_home_only(args[0])
|
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):
|
async def get_state(self):
|
||||||
"""Update HMIP state and tell Home Assistant."""
|
"""Update HMIP state and tell Home Assistant."""
|
||||||
await self.home.get_current_state()
|
await self.home.get_current_state()
|
||||||
@ -225,6 +238,7 @@ class HomematicipHAP:
|
|||||||
except HmipConnectionError:
|
except HmipConnectionError:
|
||||||
raise HmipcConnectionError
|
raise HmipcConnectionError
|
||||||
home.on_update(self.async_update)
|
home.on_update(self.async_update)
|
||||||
|
home.on_create(self.async_create_entity)
|
||||||
hass.loop.create_task(self.async_connect())
|
hass.loop.create_task(self.async_connect())
|
||||||
|
|
||||||
return home
|
return home
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/components/homematicip_cloud",
|
"documentation": "https://www.home-assistant.io/components/homematicip_cloud",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"homematicip==0.10.11"
|
"homematicip==0.10.12"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": [
|
"codeowners": [
|
||||||
|
@ -649,7 +649,7 @@ homeassistant-pyozw==0.1.4
|
|||||||
homekit[IP]==0.15.0
|
homekit[IP]==0.15.0
|
||||||
|
|
||||||
# homeassistant.components.homematicip_cloud
|
# homeassistant.components.homematicip_cloud
|
||||||
homematicip==0.10.11
|
homematicip==0.10.12
|
||||||
|
|
||||||
# homeassistant.components.horizon
|
# homeassistant.components.horizon
|
||||||
horimote==0.4.1
|
horimote==0.4.1
|
||||||
|
@ -188,7 +188,7 @@ home-assistant-frontend==20190919.1
|
|||||||
homekit[IP]==0.15.0
|
homekit[IP]==0.15.0
|
||||||
|
|
||||||
# homeassistant.components.homematicip_cloud
|
# homeassistant.components.homematicip_cloud
|
||||||
homematicip==0.10.11
|
homematicip==0.10.12
|
||||||
|
|
||||||
# homeassistant.components.google
|
# homeassistant.components.google
|
||||||
# homeassistant.components.remember_the_milk
|
# homeassistant.components.remember_the_milk
|
||||||
|
Loading…
x
Reference in New Issue
Block a user