async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: """Set up the switcher component.""" from aioswitcher.bridge import SwitcherV2Bridge phone_id = config[DOMAIN][CONF_PHONE_ID] device_id = config[DOMAIN][CONF_DEVICE_ID] device_password = config[DOMAIN][CONF_DEVICE_PASSWORD] v2bridge = SwitcherV2Bridge(hass.loop, phone_id, device_id, device_password) await v2bridge.start() async def async_stop_bridge(event: EventType) -> None: """On homeassistant stop, gracefully stop the bridge if running.""" await v2bridge.stop() hass.async_add_job( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_bridge)) try: device_data = await wait_for(v2bridge.queue.get(), timeout=5.0, loop=hass.loop) except (Asyncio_TimeoutError, RuntimeError): _LOGGER.exception("failed to get response from device") await v2bridge.stop() return False hass.data[DOMAIN] = {DATA_DEVICE: device_data} hass.async_create_task( async_load_platform(hass, SWITCH_DOMAIN, DOMAIN, None, config)) @callback def device_updates(timestamp: Optional[datetime]) -> None: """Use for updating the device data from the queue.""" if v2bridge.running: try: device_new_data = v2bridge.queue.get_nowait() if device_new_data: async_dispatcher_send(hass, SIGNAL_SWITCHER_DEVICE_UPDATE, device_new_data) except QueueEmpty: pass async_track_time_interval(hass, device_updates, timedelta(seconds=4)) return True
async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: """Set up the switcher component.""" phone_id = config[DOMAIN][CONF_PHONE_ID] device_id = config[DOMAIN][CONF_DEVICE_ID] device_password = config[DOMAIN][CONF_DEVICE_PASSWORD] v2bridge = SwitcherV2Bridge(hass.loop, phone_id, device_id, device_password) await v2bridge.start() async def async_stop_bridge(event: EventType) -> None: """On Home Assistant stop, gracefully stop the bridge if running.""" await v2bridge.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_bridge) try: device_data = await wait_for(v2bridge.queue.get(), timeout=10.0) except (Asyncio_TimeoutError, RuntimeError): _LOGGER.exception("Failed to get response from device") await v2bridge.stop() return False hass.data[DOMAIN] = {DATA_DEVICE: device_data} async def async_switch_platform_discovered( platform: str, discovery_info: DiscoveryInfoType) -> None: """Use for registering services after switch platform is discovered.""" if platform != DOMAIN: return async def async_set_auto_off_service(service: ServiceCallType) -> None: """Use for handling setting device auto-off service calls.""" await _validate_edit_permission(hass, service.context, service.data[ATTR_ENTITY_ID]) async with SwitcherV2Api(hass.loop, device_data.ip_addr, phone_id, device_id, device_password) as swapi: await swapi.set_auto_shutdown(service.data[CONF_AUTO_OFF]) async def async_turn_on_with_timer_service( service: ServiceCallType) -> None: """Use for handling turning device on with a timer service calls.""" await _validate_edit_permission(hass, service.context, service.data[ATTR_ENTITY_ID]) async with SwitcherV2Api(hass.loop, device_data.ip_addr, phone_id, device_id, device_password) as swapi: await swapi.control_device(COMMAND_ON, service.data[CONF_TIMER_MINUTES]) hass.services.async_register( DOMAIN, SERVICE_SET_AUTO_OFF_NAME, async_set_auto_off_service, schema=SERVICE_SET_AUTO_OFF_SCHEMA, ) hass.services.async_register( DOMAIN, SERVICE_TURN_ON_WITH_TIMER_NAME, async_turn_on_with_timer_service, schema=SERVICE_TURN_ON_WITH_TIMER_SCHEMA, ) async_listen_platform(hass, SWITCH_DOMAIN, async_switch_platform_discovered) hass.async_create_task( async_load_platform(hass, SWITCH_DOMAIN, DOMAIN, {}, config)) @callback def device_updates(timestamp: Optional[datetime]) -> None: """Use for updating the device data from the queue.""" if v2bridge.running: try: device_new_data = v2bridge.queue.get_nowait() if device_new_data: async_dispatcher_send(hass, SIGNAL_SWITCHER_DEVICE_UPDATE, device_new_data) except QueueEmpty: pass async_track_time_interval(hass, device_updates, timedelta(seconds=4)) return True