From 0d94324d58dd049cf2520d041a769e50368c8662 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Tue, 17 May 2022 20:57:41 +0300 Subject: [PATCH] Clean up Shelly async methods that are not awaiting (#72026) --- homeassistant/components/shelly/__init__.py | 9 ++++---- .../components/shelly/binary_sensor.py | 8 +++---- homeassistant/components/shelly/climate.py | 12 +++++----- homeassistant/components/shelly/cover.py | 10 ++++---- homeassistant/components/shelly/entity.py | 23 +++++++++++-------- homeassistant/components/shelly/light.py | 14 ++++++----- homeassistant/components/shelly/number.py | 2 +- homeassistant/components/shelly/sensor.py | 8 +++---- homeassistant/components/shelly/switch.py | 14 ++++++----- homeassistant/components/shelly/utils.py | 3 ++- 10 files changed, 58 insertions(+), 45 deletions(-) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 41a9e68fbdd..4551fee5590 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -182,7 +182,7 @@ async def async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bo data["model"] = device.settings["device"]["type"] hass.config_entries.async_update_entry(entry, data=data) - hass.async_create_task(async_block_device_setup(hass, entry, device)) + async_block_device_setup(hass, entry, device) if sleep_period == 0: # Not a sleeping device, finish setup @@ -197,7 +197,7 @@ async def async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bo except OSError as err: raise ConfigEntryNotReady(str(err) or "Error during device setup") from err - await async_block_device_setup(hass, entry, device) + async_block_device_setup(hass, entry, device) elif sleep_period is None or device_entry is None: # Need to get sleep info or first time sleeping device setup, wait for device hass.data[DOMAIN][DATA_CONFIG_ENTRY][entry.entry_id][DEVICE] = device @@ -208,12 +208,13 @@ async def async_setup_block_entry(hass: HomeAssistant, entry: ConfigEntry) -> bo else: # Restore sensors for sleeping device LOGGER.debug("Setting up offline block device %s", entry.title) - await async_block_device_setup(hass, entry, device) + async_block_device_setup(hass, entry, device) return True -async def async_block_device_setup( +@callback +def async_block_device_setup( hass: HomeAssistant, entry: ConfigEntry, device: BlockDevice ) -> None: """Set up a block based device that is online.""" diff --git a/homeassistant/components/shelly/binary_sensor.py b/homeassistant/components/shelly/binary_sensor.py index a6cde0c4670..b33947ad6b7 100644 --- a/homeassistant/components/shelly/binary_sensor.py +++ b/homeassistant/components/shelly/binary_sensor.py @@ -235,12 +235,12 @@ async def async_setup_entry( ) -> None: """Set up sensors for device.""" if get_device_entry_gen(config_entry) == 2: - return await async_setup_entry_rpc( + return async_setup_entry_rpc( hass, config_entry, async_add_entities, RPC_SENSORS, RpcBinarySensor ) if config_entry.data[CONF_SLEEP_PERIOD]: - await async_setup_entry_attribute_entities( + async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, @@ -249,7 +249,7 @@ async def async_setup_entry( _build_block_description, ) else: - await async_setup_entry_attribute_entities( + async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, @@ -257,7 +257,7 @@ async def async_setup_entry( BlockBinarySensor, _build_block_description, ) - await async_setup_entry_rest( + async_setup_entry_rest( hass, config_entry, async_add_entities, diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index a042254fdc6..453d67f39a7 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -50,14 +50,13 @@ async def async_setup_entry( ][BLOCK] if wrapper.device.initialized: - await async_setup_climate_entities(async_add_entities, wrapper) + async_setup_climate_entities(async_add_entities, wrapper) else: - await async_restore_climate_entities( - hass, config_entry, async_add_entities, wrapper - ) + async_restore_climate_entities(hass, config_entry, async_add_entities, wrapper) -async def async_setup_climate_entities( +@callback +def async_setup_climate_entities( async_add_entities: AddEntitiesCallback, wrapper: BlockDeviceWrapper, ) -> None: @@ -79,7 +78,8 @@ async def async_setup_climate_entities( async_add_entities([BlockSleepingClimate(wrapper, sensor_block, device_block)]) -async def async_restore_climate_entities( +@callback +def async_restore_climate_entities( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index 9bc61f9415f..e28fe22a528 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -28,12 +28,13 @@ async def async_setup_entry( ) -> None: """Set up switches for device.""" if get_device_entry_gen(config_entry) == 2: - return await async_setup_rpc_entry(hass, config_entry, async_add_entities) + return async_setup_rpc_entry(hass, config_entry, async_add_entities) - return await async_setup_block_entry(hass, config_entry, async_add_entities) + return async_setup_block_entry(hass, config_entry, async_add_entities) -async def async_setup_block_entry( +@callback +def async_setup_block_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -48,7 +49,8 @@ async def async_setup_block_entry( async_add_entities(BlockShellyCover(wrapper, block) for block in blocks) -async def async_setup_rpc_entry( +@callback +def async_setup_rpc_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index de7d3dd9437..8cba4d2804e 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -46,7 +46,8 @@ from .utils import ( ) -async def async_setup_entry_attribute_entities( +@callback +def async_setup_entry_attribute_entities( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -62,11 +63,11 @@ async def async_setup_entry_attribute_entities( ][BLOCK] if wrapper.device.initialized: - await async_setup_block_attribute_entities( + async_setup_block_attribute_entities( hass, async_add_entities, wrapper, sensors, sensor_class ) else: - await async_restore_block_attribute_entities( + async_restore_block_attribute_entities( hass, config_entry, async_add_entities, @@ -77,7 +78,8 @@ async def async_setup_entry_attribute_entities( ) -async def async_setup_block_attribute_entities( +@callback +def async_setup_block_attribute_entities( hass: HomeAssistant, async_add_entities: AddEntitiesCallback, wrapper: BlockDeviceWrapper, @@ -105,7 +107,7 @@ async def async_setup_block_attribute_entities( ): domain = sensor_class.__module__.split(".")[-1] unique_id = f"{wrapper.mac}-{block.description}-{sensor_id}" - await async_remove_shelly_entity(hass, domain, unique_id) + async_remove_shelly_entity(hass, domain, unique_id) else: blocks.append((block, sensor_id, description)) @@ -120,7 +122,8 @@ async def async_setup_block_attribute_entities( ) -async def async_restore_block_attribute_entities( +@callback +def async_restore_block_attribute_entities( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -158,7 +161,8 @@ async def async_restore_block_attribute_entities( async_add_entities(entities) -async def async_setup_entry_rpc( +@callback +def async_setup_entry_rpc( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -192,7 +196,7 @@ async def async_setup_entry_rpc( ): domain = sensor_class.__module__.split(".")[-1] unique_id = f"{wrapper.mac}-{key}-{sensor_id}" - await async_remove_shelly_entity(hass, domain, unique_id) + async_remove_shelly_entity(hass, domain, unique_id) else: if description.use_polling_wrapper: entities.append( @@ -207,7 +211,8 @@ async def async_setup_entry_rpc( async_add_entities(entities) -async def async_setup_entry_rest( +@callback +def async_setup_entry_rest( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index 295f64b01f6..79db9c509f4 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -65,12 +65,13 @@ async def async_setup_entry( ) -> None: """Set up lights for device.""" if get_device_entry_gen(config_entry) == 2: - return await async_setup_rpc_entry(hass, config_entry, async_add_entities) + return async_setup_rpc_entry(hass, config_entry, async_add_entities) - return await async_setup_block_entry(hass, config_entry, async_add_entities) + return async_setup_block_entry(hass, config_entry, async_add_entities) -async def async_setup_block_entry( +@callback +def async_setup_block_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -92,7 +93,7 @@ async def async_setup_block_entry( blocks.append(block) assert wrapper.device.shelly unique_id = f"{wrapper.mac}-{block.type}_{block.channel}" - await async_remove_shelly_entity(hass, "switch", unique_id) + async_remove_shelly_entity(hass, "switch", unique_id) if not blocks: return @@ -100,7 +101,8 @@ async def async_setup_block_entry( async_add_entities(BlockShellyLight(wrapper, block) for block in blocks) -async def async_setup_rpc_entry( +@callback +def async_setup_rpc_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -116,7 +118,7 @@ async def async_setup_rpc_entry( switch_ids.append(id_) unique_id = f"{wrapper.mac}-switch:{id_}" - await async_remove_shelly_entity(hass, "switch", unique_id) + async_remove_shelly_entity(hass, "switch", unique_id) if not switch_ids: return diff --git a/homeassistant/components/shelly/number.py b/homeassistant/components/shelly/number.py index bfac4cd4033..dcedc32602d 100644 --- a/homeassistant/components/shelly/number.py +++ b/homeassistant/components/shelly/number.py @@ -81,7 +81,7 @@ async def async_setup_entry( return if config_entry.data[CONF_SLEEP_PERIOD]: - await async_setup_entry_attribute_entities( + async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 7e19b9724d7..92c19734414 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -394,12 +394,12 @@ async def async_setup_entry( ) -> None: """Set up sensors for device.""" if get_device_entry_gen(config_entry) == 2: - return await async_setup_entry_rpc( + return async_setup_entry_rpc( hass, config_entry, async_add_entities, RPC_SENSORS, RpcSensor ) if config_entry.data[CONF_SLEEP_PERIOD]: - await async_setup_entry_attribute_entities( + async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, @@ -408,7 +408,7 @@ async def async_setup_entry( _build_block_description, ) else: - await async_setup_entry_attribute_entities( + async_setup_entry_attribute_entities( hass, config_entry, async_add_entities, @@ -416,7 +416,7 @@ async def async_setup_entry( BlockSensor, _build_block_description, ) - await async_setup_entry_rest( + async_setup_entry_rest( hass, config_entry, async_add_entities, REST_SENSORS, RestSensor ) diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py index 9114587a910..d65568d0a2a 100644 --- a/homeassistant/components/shelly/switch.py +++ b/homeassistant/components/shelly/switch.py @@ -29,12 +29,13 @@ async def async_setup_entry( ) -> None: """Set up switches for device.""" if get_device_entry_gen(config_entry) == 2: - return await async_setup_rpc_entry(hass, config_entry, async_add_entities) + return async_setup_rpc_entry(hass, config_entry, async_add_entities) - return await async_setup_block_entry(hass, config_entry, async_add_entities) + return async_setup_block_entry(hass, config_entry, async_add_entities) -async def async_setup_block_entry( +@callback +def async_setup_block_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -59,7 +60,7 @@ async def async_setup_block_entry( relay_blocks.append(block) unique_id = f"{wrapper.mac}-{block.type}_{block.channel}" - await async_remove_shelly_entity(hass, "light", unique_id) + async_remove_shelly_entity(hass, "light", unique_id) if not relay_blocks: return @@ -67,7 +68,8 @@ async def async_setup_block_entry( async_add_entities(BlockRelaySwitch(wrapper, block) for block in relay_blocks) -async def async_setup_rpc_entry( +@callback +def async_setup_rpc_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, @@ -84,7 +86,7 @@ async def async_setup_rpc_entry( switch_ids.append(id_) unique_id = f"{wrapper.mac}-switch:{id_}" - await async_remove_shelly_entity(hass, "light", unique_id) + async_remove_shelly_entity(hass, "light", unique_id) if not switch_ids: return diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 0398250df71..6dfc2fb3be8 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -30,7 +30,8 @@ from .const import ( ) -async def async_remove_shelly_entity( +@callback +def async_remove_shelly_entity( hass: HomeAssistant, domain: str, unique_id: str ) -> None: """Remove a Shelly entity."""