- added logic to prevent double registration of entities

This commit is contained in:
Jack Simbach 2022-09-17 11:05:58 -04:00
parent f2553c9bbc
commit 992623bb94
9 changed files with 38 additions and 23 deletions

View File

@ -1,5 +1,4 @@
"""GE Home Sensor Entities"""
import async_timeout
import logging
from typing import Callable
@ -7,6 +6,7 @@ from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .devices import ApplianceApi
@ -20,7 +20,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
_LOGGER.debug('Adding GE Binary Sensor Entities')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
#apis = coordinator.appliance_apis.values()
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -31,8 +31,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeErdBinarySensor) and not isinstance(entity, SwitchEntity)
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} binary sensors')
_LOGGER.debug(f'Found {len(entities):d} unregistered binary sensors')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,11 +1,11 @@
"""GE Home Button Entities"""
import async_timeout
import logging
from typing import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .devices import ApplianceApi
@ -19,6 +19,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
_LOGGER.debug('Adding GE Button Entities')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -28,8 +29,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeErdButton)
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} buttons ')
_LOGGER.debug(f'Found {len(entities):d} unregistered buttons ')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,5 +1,4 @@
"""GE Home Climate Entities"""
import async_timeout
import logging
from typing import Callable
@ -7,6 +6,7 @@ from homeassistant.components.climate import ClimateEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .entities import GeClimate
from .const import DOMAIN
@ -20,17 +20,20 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
_LOGGER.debug('Adding GE Climate Entities')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
_LOGGER.debug(f'Found {len(apis):d} appliance APIs')
entities = [
entity
for api in apis
for entity in api.entities
if isinstance(entity, GeClimate)
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} climate entities')
_LOGGER.debug(f'Found {len(entities):d} unregistered climate entities')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,12 +1,11 @@
"""GE Home Select Entities"""
import async_timeout
import logging
from typing import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .entities import GeErdLight
@ -22,6 +21,7 @@ async def async_setup_entry(
"""GE Home lights."""
_LOGGER.debug("Adding GE Home lights")
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -32,8 +32,9 @@ async def async_setup_entry(
for entity in api.entities
if isinstance(entity, GeErdLight)
and entity.erd_code in api.appliance._property_cache
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f"Found {len(entities):d} lights")
_LOGGER.debug(f"Found {len(entities):d} unregistered lights")
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,12 +1,11 @@
"""GE Home Number Entities"""
import async_timeout
import logging
from typing import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .devices import ApplianceApi
@ -20,6 +19,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
_LOGGER.debug('Adding GE Number Entities')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -29,8 +29,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeErdNumber)
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} numbers ')
_LOGGER.debug(f'Found {len(entities):d} unregisterd numbers')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,11 +1,11 @@
"""GE Home Select Entities"""
import async_timeout
import logging
from typing import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .const import DOMAIN
from .devices import ApplianceApi
@ -21,6 +21,7 @@ async def async_setup_entry(
"""GE Home selects."""
_LOGGER.debug("Adding GE Home selects")
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -31,8 +32,9 @@ async def async_setup_entry(
for entity in api.entities
if isinstance(entity, GeErdSelect)
and entity.erd_code in api.appliance._property_cache
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f"Found {len(entities):d} selectors")
_LOGGER.debug(f"Found {len(entities):d} unregistered selects")
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,5 +1,4 @@
"""GE Home Sensor Entities"""
import async_timeout
import logging
from typing import Callable
import voluptuous as vol
@ -9,6 +8,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_platform
from homeassistant.helpers import entity_registry as er
from .const import (
DOMAIN,
@ -29,7 +29,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
"""GE Home sensors."""
_LOGGER.debug('Adding GE Home sensors')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
# Get the platform
platform = entity_platform.async_get_current_platform()
@ -41,8 +42,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeErdSensor) and entity.erd_code in api.appliance._property_cache
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} sensors')
_LOGGER.debug(f'Found {len(entities):d} unregistered sensors')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -1,12 +1,11 @@
"""GE Home Switch Entities"""
import async_timeout
import logging
from typing import Callable
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .entities import GeErdSwitch
from .const import DOMAIN
@ -19,6 +18,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
"""GE Home sensors."""
_LOGGER.debug('Adding GE Home switches')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -28,8 +28,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeErdSwitch) and entity.erd_code in api.appliance._property_cache
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} switches')
_LOGGER.debug(f'Found {len(entities):d} unregistered switches')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)

View File

@ -7,7 +7,7 @@ from homeassistant.components.water_heater import WaterHeaterEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from .entities import GeAbstractWaterHeater
from .const import DOMAIN
@ -20,6 +20,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
"""GE Home Water Heaters."""
_LOGGER.debug('Adding GE "Water Heaters"')
coordinator: GeHomeUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
registry = er.async_get(hass)
@callback
def async_devices_discovered(apis: list[ApplianceApi]):
@ -29,8 +30,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
for api in apis
for entity in api.entities
if isinstance(entity, GeAbstractWaterHeater)
if not registry.async_is_registered(entity.entity_id)
]
_LOGGER.debug(f'Found {len(entities):d} "water heaters"')
_LOGGER.debug(f'Found {len(entities):d} unregistered water heaters')
async_add_entities(entities)
async_dispatcher_connect(hass, coordinator.signal_ready, async_devices_discovered)