async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up the Tibber sensor.""" tibber_connection = hass.data[TIBBER_DOMAIN] entity_registry = async_get_entity_reg(hass) device_registry = async_get_dev_reg(hass) coordinator: TibberDataCoordinator | None = None entities: list[TibberSensor] = [] for home in tibber_connection.get_homes(only_active=False): try: await home.update_info() except asyncio.TimeoutError as err: _LOGGER.error("Timeout connecting to Tibber home: %s ", err) raise PlatformNotReady() from err except aiohttp.ClientError as err: _LOGGER.error("Error connecting to Tibber home: %s ", err) raise PlatformNotReady() from err if home.has_active_subscription: entities.append(TibberSensorElPrice(home)) if coordinator is None: coordinator = TibberDataCoordinator(hass, tibber_connection) for entity_description in SENSORS: entities.append( TibberDataSensor(home, coordinator, entity_description)) if home.has_real_time_consumption: await home.rt_subscribe( TibberRtDataCoordinator(async_add_entities, home, hass).async_set_updated_data) # migrate old_id = home.info["viewer"]["home"]["meteringPointData"][ "consumptionEan"] if old_id is None: continue # migrate to new device ids old_entity_id = entity_registry.async_get_entity_id( "sensor", TIBBER_DOMAIN, old_id) if old_entity_id is not None: entity_registry.async_update_entity(old_entity_id, new_unique_id=home.home_id) # migrate to new device ids device_entry = device_registry.async_get_device({(TIBBER_DOMAIN, old_id)}) if device_entry and entry.entry_id in device_entry.config_entries: device_registry.async_update_device(device_entry.id, new_identifiers={ (TIBBER_DOMAIN, home.home_id) }) async_add_entities(entities, True)
async def async_setup_entry(hass, entry, async_add_entities): """Set up the Tibber sensor.""" tibber_connection = hass.data.get(TIBBER_DOMAIN) entity_registry = async_get_entity_reg(hass) device_registry = async_get_dev_reg(hass) coordinator = None entities = [] for home in tibber_connection.get_homes(only_active=False): try: await home.update_info() except asyncio.TimeoutError as err: _LOGGER.error("Timeout connecting to Tibber home: %s ", err) raise PlatformNotReady() from err except aiohttp.ClientError as err: _LOGGER.error("Error connecting to Tibber home: %s ", err) raise PlatformNotReady() from err if home.has_active_subscription: entities.append(TibberSensorElPrice(home)) if home.has_real_time_consumption: await home.rt_subscribe( TibberRtDataCoordinator(async_add_entities, home, hass).async_set_updated_data) if home.has_active_subscription and not home.has_real_time_consumption: if coordinator is None: coordinator = update_coordinator.DataUpdateCoordinator( hass, _LOGGER, name=f"Tibber {tibber_connection.name}", update_method=tibber_connection. fetch_consumption_data_active_homes, update_interval=timedelta(hours=1), ) for entity_description in SENSORS: entities.append( TibberDataSensor(home, coordinator, entity_description)) # migrate old_id = home.info["viewer"]["home"]["meteringPointData"][ "consumptionEan"] if old_id is None: continue # migrate to new device ids old_entity_id = entity_registry.async_get_entity_id( "sensor", TIBBER_DOMAIN, old_id) if old_entity_id is not None: entity_registry.async_update_entity(old_entity_id, new_unique_id=home.home_id) # migrate to new device ids device_entry = device_registry.async_get_device({(TIBBER_DOMAIN, old_id)}) if device_entry and entry.entry_id in device_entry.config_entries: device_registry.async_update_device(device_entry.id, new_identifiers={ (TIBBER_DOMAIN, home.home_id) }) async_add_entities(entities, True)