Move Xiaomi Miio services to async_setup - #175484
Merged
joostlek merged 4 commits intoJul 6, 2026
Merged
Conversation
erwindouna
requested review from
rytilahti,
starkillerOG and
syssi
as code owners
July 3, 2026 11:26
Contributor
|
Hey there @rytilahti, @syssi, @starkillerOG, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
epenet
marked this pull request as draft
July 3, 2026 12:35
erwindouna
marked this pull request as ready for review
July 3, 2026 18:19
epenet
previously requested changes
Jul 6, 2026
Comment on lines
123
to
124
| # Vacuum Services | ||
| service.async_register_platform_entity_service( |
Contributor
There was a problem hiding this comment.
_async_setup_fan_services
_async_setup_light_services
_async_setup_switch_services
...
# Vacuum Services
service.async_register_platform_entity_service(
Contributor
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
| ) | ||
| from .typing import ServiceMethodDetails | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) |
Comment on lines
+228
to
+337
| def _async_setup_light_services(hass: HomeAssistant) -> None: | ||
| """Set up Xiaomi Miio light services.""" | ||
| hass.data.setdefault(LIGHT_DATA_KEY, {}) | ||
|
|
||
| async def async_service_handler(call: ServiceCall) -> None: | ||
| """Map services to methods on Xiaomi Philips Lights.""" | ||
| method = LIGHT_SERVICE_TO_METHOD[call.service] | ||
| params = { | ||
| key: value for key, value in call.data.items() if key != ATTR_ENTITY_ID | ||
| } | ||
| if entity_ids := call.data.get(ATTR_ENTITY_ID): | ||
| target_devices = [ | ||
| dev | ||
| for dev in hass.data[LIGHT_DATA_KEY].values() | ||
| if dev.entity_id in entity_ids | ||
| ] | ||
| else: | ||
| target_devices = hass.data[LIGHT_DATA_KEY].values() | ||
|
|
||
| update_tasks = [] | ||
| for target_device in target_devices: | ||
| if not hasattr(target_device, method.method): | ||
| continue | ||
| await getattr(target_device, method.method)(**params) | ||
| update_tasks.append( | ||
| asyncio.create_task(target_device.async_update_ha_state(True)) | ||
| ) | ||
|
|
||
| if update_tasks: | ||
| await asyncio.wait(update_tasks) | ||
|
|
||
| for xiaomi_miio_service, method in LIGHT_SERVICE_TO_METHOD.items(): | ||
| schema = method.schema or XIAOMI_MIIO_SERVICE_SCHEMA | ||
| hass.services.async_register( | ||
| DOMAIN, xiaomi_miio_service, async_service_handler, schema=schema | ||
| ) | ||
|
|
||
|
|
||
| def _async_setup_switch_services(hass: HomeAssistant) -> None: | ||
| """Set up Xiaomi Miio switch services.""" | ||
| hass.data.setdefault(SWITCH_DATA_KEY, {}) | ||
|
|
||
| async def async_service_handler(call: ServiceCall) -> None: | ||
| """Map services to methods on XiaomiPlugGenericSwitch.""" | ||
| method = SWITCH_SERVICE_TO_METHOD[call.service] | ||
| params = { | ||
| key: value for key, value in call.data.items() if key != ATTR_ENTITY_ID | ||
| } | ||
| if entity_ids := call.data.get(ATTR_ENTITY_ID): | ||
| devices = [ | ||
| device | ||
| for device in hass.data[SWITCH_DATA_KEY].values() | ||
| if device.entity_id in entity_ids | ||
| ] | ||
| else: | ||
| devices = hass.data[SWITCH_DATA_KEY].values() | ||
|
|
||
| update_tasks = [] | ||
| for device in devices: | ||
| if not hasattr(device, method.method): | ||
| continue | ||
| await getattr(device, method.method)(**params) | ||
| update_tasks.append(asyncio.create_task(device.async_update_ha_state(True))) | ||
|
|
||
| if update_tasks: | ||
| await asyncio.wait(update_tasks) | ||
|
|
||
| for plug_service, method in SWITCH_SERVICE_TO_METHOD.items(): | ||
| schema = method.schema or SWITCH_SERVICE_SCHEMA | ||
| hass.services.async_register( | ||
| DOMAIN, plug_service, async_service_handler, schema=schema | ||
| ) | ||
|
|
||
|
|
||
| def _async_setup_fan_services(hass: HomeAssistant) -> None: | ||
| """Set up Xiaomi Miio fan services.""" | ||
| hass.data.setdefault(FAN_DATA_KEY, {}) | ||
|
|
||
| async def async_service_handler(call: ServiceCall) -> None: | ||
| """Map services to methods on XiaomiAirPurifier.""" | ||
| method = FAN_SERVICE_TO_METHOD[call.service] | ||
| params = { | ||
| key: value for key, value in call.data.items() if key != ATTR_ENTITY_ID | ||
| } | ||
| if entity_ids := call.data.get(ATTR_ENTITY_ID): | ||
| filtered_entities = [ | ||
| entity | ||
| for entity in hass.data[FAN_DATA_KEY].values() | ||
| if entity.entity_id in entity_ids | ||
| ] | ||
| else: | ||
| filtered_entities = hass.data[FAN_DATA_KEY].values() | ||
|
|
||
| update_tasks = [] | ||
|
|
||
| for entity in filtered_entities: | ||
| entity_method = getattr(entity, method.method, None) | ||
| if not entity_method: | ||
| continue | ||
| await entity_method(**params) | ||
| update_tasks.append(asyncio.create_task(entity.async_update_ha_state(True))) | ||
|
|
||
| if update_tasks: | ||
| await asyncio.wait(update_tasks) | ||
|
|
||
| for air_purifier_service, method in FAN_SERVICE_TO_METHOD.items(): | ||
| schema = method.schema or FAN_SERVICE_SCHEMA | ||
| hass.services.async_register( | ||
| DOMAIN, air_purifier_service, async_service_handler, schema=schema | ||
| ) |
joostlek
approved these changes
Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change
Proposed change
Move Xiaomi Miio service registration from async_setup_entry to async_setup.
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: