async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up Sensibo devices.""" client = pysensibo.SensiboClient( config[CONF_API_KEY], session=async_get_clientsession(hass), timeout=TIMEOUT ) devices = [] try: with async_timeout.timeout(TIMEOUT): for dev in await client.async_get_devices(_INITIAL_FETCH_FIELDS): if config[CONF_ID] == ALL or dev["id"] in config[CONF_ID]: devices.append( SensiboClimate(client, dev, hass.config.units.temperature_unit) ) except ( aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, pysensibo.SensiboError, ) as err: _LOGGER.error("Failed to get devices from Sensibo servers") raise PlatformNotReady from err if not devices: return async_add_entities(devices) async def async_assume_state(service): """Set state according to external service call..""" if entity_ids := service.data.get(ATTR_ENTITY_ID): target_climate = [ device for device in devices if device.entity_id in entity_ids ] else:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Sensibo from a config entry.""" client = pysensibo.SensiboClient(entry.data[CONF_API_KEY], session=async_get_clientsession(hass), timeout=TIMEOUT) devices = [] try: async with async_timeout.timeout(TIMEOUT): for dev in await client.async_get_devices(_INITIAL_FETCH_FIELDS): devices.append(dev) except ( aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, pysensibo.SensiboError, ) as err: raise ConfigEntryNotReady( f"Failed to get devices from Sensibo servers: {err}") from err if not devices: return False hass.data.setdefault(DOMAIN, {})[entry.entry_id] = { "devices": devices, "client": client, } hass.config_entries.async_setup_platforms(entry, PLATFORMS) _LOGGER.debug("Loaded entry for %s", entry.title) return True
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up Sensibo devices.""" client = pysensibo.SensiboClient(config[CONF_API_KEY], session=async_get_clientsession(hass), timeout=TIMEOUT) devices = [] try: with async_timeout.timeout(TIMEOUT): for dev in await client.async_get_devices(_INITIAL_FETCH_FIELDS): if config[CONF_ID] == ALL or dev["id"] in config[CONF_ID]: devices.append( SensiboClimate(client, dev, hass.config.units.temperature_unit)) except ( aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, pysensibo.SensiboError, ) as err: _LOGGER.error("Failed to get devices from Sensibo servers") raise PlatformNotReady from err if not devices: return async_add_entities(devices) async def async_assume_state(service): """Set state according to external service call..""" entity_ids = service.data.get(ATTR_ENTITY_ID) if entity_ids: target_climate = [ device for device in devices if device.entity_id in entity_ids ] else: target_climate = devices update_tasks = [] for climate in target_climate: await climate.async_assume_state(service.data.get(ATTR_STATE)) update_tasks.append(climate.async_update_ha_state(True)) if update_tasks: await asyncio.wait(update_tasks) hass.services.async_register( SENSIBO_DOMAIN, SERVICE_ASSUME_STATE, async_assume_state, schema=ASSUME_STATE_SCHEMA, )
def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up Sensibo devices.""" import pysensibo client = pysensibo.SensiboClient(config[CONF_API_KEY], session=async_get_clientsession(hass), timeout=TIMEOUT) devices = [] try: for dev in (yield from client.async_get_devices(_INITIAL_FETCH_FIELDS)): if config[CONF_ID] == ALL or dev['id'] in config[CONF_ID]: devices.append( SensiboClimate(client, dev, hass.config.units.temperature_unit)) except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError): _LOGGER.exception('Failed to connect to Sensibo servers.') raise PlatformNotReady if devices: async_add_entities(devices) @asyncio.coroutine def async_assume_state(service): """Set state according to external service call..""" entity_ids = service.data.get(ATTR_ENTITY_ID) if entity_ids: target_climate = [ device for device in devices if device.entity_id in entity_ids ] else: target_climate = devices update_tasks = [] for climate in target_climate: yield from climate.async_assume_state( service.data.get(ATTR_STATE)) update_tasks.append(climate.async_update_ha_state(True)) if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop) hass.services.async_register(DOMAIN, SERVICE_ASSUME_STATE, async_assume_state, schema=ASSUME_STATE_SCHEMA)
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up Sensibo devices.""" import pysensibo client = pysensibo.SensiboClient(config[CONF_API_KEY], session=async_get_clientsession(hass), timeout=TIMEOUT) devices = [] try: for dev in (yield from client.async_get_devices(_INITIAL_FETCH_FIELDS)): if config[CONF_ID] == ALL or dev['id'] in config[CONF_ID]: devices.append(SensiboClimate(client, dev)) except aiohttp.client_exceptions.ClientConnectorError: _LOGGER.exception('Failed to connct to Sensibo servers.') return False if devices: async_add_devices(devices)