Skip to content

Move Xiaomi Miio services to async_setup - #175484

Merged
joostlek merged 4 commits into
home-assistant:devfrom
erwindouna:xiaomi_miio_move_services_to_async_setup
Jul 6, 2026
Merged

Move Xiaomi Miio services to async_setup#175484
joostlek merged 4 commits into
home-assistant:devfrom
erwindouna:xiaomi_miio_move_services_to_async_setup

Conversation

@erwindouna

@erwindouna erwindouna commented Jul 3, 2026

Copy link
Copy Markdown
Member

Breaking change

Proposed change

Move Xiaomi Miio service registration from async_setup_entry to async_setup.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings July 3, 2026 11:26
@home-assistant home-assistant Bot added cla-signed code-quality integration: xiaomi_miio Top 200 Integration is ranked within the top 200 by usage labels Jul 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@home-assistant

home-assistant Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Hey there @rytilahti, @syssi, @starkillerOG, mind taking a look at this pull request as it has been labeled with an integration (xiaomi_miio) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of xiaomi_miio can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign xiaomi_miio Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@epenet
epenet marked this pull request as draft July 3, 2026 12:35
@erwindouna
erwindouna marked this pull request as ready for review July 3, 2026 18:19
epenet
epenet previously requested changes Jul 6, 2026
Comment thread homeassistant/components/xiaomi_miio/__init__.py Outdated
Comment thread homeassistant/components/xiaomi_miio/services.py Outdated
Comment thread homeassistant/components/xiaomi_miio/services.py Outdated
Comment thread homeassistant/components/xiaomi_miio/services.py Outdated
Comment thread homeassistant/components/xiaomi_miio/services.py Outdated
Comment on lines 123 to 124
# Vacuum Services
service.async_register_platform_entity_service(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    _async_setup_fan_services
    _async_setup_light_services
    _async_setup_switch_services
    ...
    # Vacuum Services
    service.async_register_platform_entity_service(

@home-assistant
home-assistant Bot marked this pull request as draft July 6, 2026 08:56
@home-assistant

home-assistant Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

erwindouna and others added 2 commits July 6, 2026 11:07
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
@erwindouna
erwindouna marked this pull request as ready for review July 6, 2026 09:22
@home-assistant
home-assistant Bot requested a review from epenet July 6, 2026 09:23
@frenck
frenck requested a review from Copilot July 6, 2026 10:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

)
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
joostlek merged commit 2b690a3 into home-assistant:dev Jul 6, 2026
33 checks passed
@erwindouna
erwindouna deleted the xiaomi_miio_move_services_to_async_setup branch July 6, 2026 11:36
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move service registration to async_setup in xiaomi_miio

7 participants