Ejemplo n.º 1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up lookin from a config entry."""

    host = entry.data[CONF_HOST]
    lookin_protocol = LookInHttpProtocol(api_uri=f"http://{host}",
                                         session=async_get_clientsession(hass))

    try:
        lookin_device = await lookin_protocol.get_info()
        devices = await lookin_protocol.get_devices()
    except (asyncio.TimeoutError, aiohttp.ClientError) as ex:
        raise ConfigEntryNotReady from ex

    meteo_coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
        hass,
        LOGGER,
        name=entry.title,
        update_method=lookin_protocol.get_meteo_sensor,
        update_interval=timedelta(
            minutes=5),  # Updates are pushed (fallback is polling)
    )
    await meteo_coordinator.async_config_entry_first_refresh()

    @callback
    def _async_meteo_push_update(msg: dict[str, str]) -> None:
        """Process an update pushed via UDP."""
        if int(msg["event_id"]):
            return
        LOGGER.debug("Processing push message for meteo sensor: %s", msg)
        meteo: MeteoSensor = meteo_coordinator.data
        meteo.update_from_value(msg["value"])
        meteo_coordinator.async_set_updated_data(meteo)

    lookin_udp_subs = LookinUDPSubscriptions()
    entry.async_on_unload(
        lookin_udp_subs.subscribe_sensor(lookin_device.id, SensorID.Meteo,
                                         None, _async_meteo_push_update))
    entry.async_on_unload(await start_lookin_udp(lookin_udp_subs))

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LookinData(
        lookin_udp_subs=lookin_udp_subs,
        lookin_device=lookin_device,
        meteo_coordinator=meteo_coordinator,
        devices=devices,
        lookin_protocol=lookin_protocol,
    )

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Ejemplo n.º 2
0
 async def async_get_subscriptions(self) -> LookinUDPSubscriptions:
     """Get the shared LookinUDPSubscriptions."""
     async with self._lock:
         if not self._listener:
             self._subscriptions = LookinUDPSubscriptions()
             self._listener = await start_lookin_udp(self._subscriptions, None)
         return self._subscriptions
Ejemplo n.º 3
0
            update_method=updater,
            update_interval=timedelta(
                seconds=60),  # Updates are pushed (fallback is polling)
        )
        await coordinator.async_config_entry_first_refresh()
        device_coordinators[uuid] = coordinator

    @callback
    def _async_meteo_push_update(event: UDPEvent) -> None:
        """Process an update pushed via UDP."""
        LOGGER.debug("Processing push message for meteo sensor: %s", event)
        meteo: MeteoSensor = meteo_coordinator.data
        meteo.update_from_value(event.value)
        meteo_coordinator.async_set_updated_data(meteo)

    lookin_udp_subs = LookinUDPSubscriptions()
    entry.async_on_unload(
        lookin_udp_subs.subscribe_event(lookin_device.id, UDPCommandType.meteo,
                                        None, _async_meteo_push_update))

    entry.async_on_unload(await start_lookin_udp(lookin_udp_subs,
                                                 lookin_device.id))

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LookinData(
        lookin_udp_subs=lookin_udp_subs,
        lookin_device=lookin_device,
        meteo_coordinator=meteo_coordinator,
        devices=devices,
        lookin_protocol=lookin_protocol,
        device_coordinators=device_coordinators,
    )