async def async_get_mac(hass: HomeAssistant, host: str, data: dict[str, Any]) -> str: """Get device MAC address.""" websession = async_get_clientsession(hass) options = ConnectionOptions(host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)) nam = await NettigoAirMonitor.create(websession, options) async with async_timeout.timeout(10): return await nam.async_get_mac_address()
async def async_get_config(hass: HomeAssistant, host: str) -> NamConfig: """Get device MAC address and auth_enabled property.""" websession = async_get_clientsession(hass) options = ConnectionOptions(host) nam = await NettigoAirMonitor.create(websession, options) async with async_timeout.timeout(10): mac = await nam.async_get_mac_address() return NamConfig(mac, nam.auth_enabled)
async def async_check_credentials(hass: HomeAssistant, host: str, data: dict[str, Any]) -> None: """Check if credentials are valid.""" websession = async_get_clientsession(hass) options = ConnectionOptions(host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)) nam = await NettigoAirMonitor.create(websession, options) async with async_timeout.timeout(10): await nam.async_check_credentials()
async def async_get_mac(hass: HomeAssistant, host: str, data: dict[str, Any]) -> str: """Get device MAC address.""" websession = async_get_clientsession(hass) options = ConnectionOptions(host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD)) nam = await NettigoAirMonitor.create(websession, options) # Device firmware uses synchronous code and doesn't respond to http queries # when reading data from sensors. The nettigo-air-monitor library tries to get # the data 4 times, so we use a longer than usual timeout here. async with async_timeout.timeout(30): return await nam.async_get_mac_address()
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Nettigo as config entry.""" host: str = entry.data[CONF_HOST] username: str | None = entry.data.get(CONF_USERNAME) password: str | None = entry.data.get(CONF_PASSWORD) websession = async_get_clientsession(hass) options = ConnectionOptions(host=host, username=username, password=password) try: nam = await NettigoAirMonitor.create(websession, options) except (ApiError, ClientError, ClientConnectorError, asyncio.TimeoutError) as err: raise ConfigEntryNotReady from err try: await nam.async_check_credentials() except AuthFailed as err: raise ConfigEntryAuthFailed from err coordinator = NAMDataUpdateCoordinator(hass, nam, entry.unique_id) await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) # Remove air_quality entities from registry if they exist ent_reg = entity_registry.async_get(hass) for sensor_type in ("sds", ATTR_SDS011, ATTR_SPS30): unique_id = f"{coordinator.unique_id}-{sensor_type}" if entity_id := ent_reg.async_get_entity_id(AIR_QUALITY_PLATFORM, DOMAIN, unique_id): _LOGGER.debug("Removing deprecated air_quality entity %s", entity_id) ent_reg.async_remove(entity_id)