Skip to content

Commit

Permalink
Add lazy discover config option to xiaomi_miio (#59215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwogu committed Nov 24, 2022
1 parent 1572221 commit 56642b9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
25 changes: 15 additions & 10 deletions homeassistant/components/xiaomi_miio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
CONF_DEVICE,
CONF_FLOW_TYPE,
CONF_GATEWAY,
CONF_LAZY_DISCOVER,
DOMAIN,
KEY_COORDINATOR,
KEY_DEVICE,
Expand Down Expand Up @@ -286,6 +287,7 @@ async def async_create_miio_device_and_coordinator(
name = entry.title
device: MiioDevice | None = None
migrate = False
lazy_discover = entry.options.get(CONF_LAZY_DISCOVER, True)
update_method = _async_update_data_default
coordinator_class: type[DataUpdateCoordinator] = DataUpdateCoordinator

Expand All @@ -302,38 +304,41 @@ async def async_create_miio_device_and_coordinator(

# Humidifiers
if model in MODELS_HUMIDIFIER_MIOT:
device = AirHumidifierMiot(host, token)
device = AirHumidifierMiot(host, token, lazy_discover=lazy_discover)
migrate = True
elif model in MODELS_HUMIDIFIER_MJJSQ:
device = AirHumidifierMjjsq(host, token, model=model)
device = AirHumidifierMjjsq(
host, token, lazy_discover=lazy_discover, model=model
)
migrate = True
elif model in MODELS_HUMIDIFIER_MIIO:
device = AirHumidifier(host, token, model=model)
device = AirHumidifier(host, token, lazy_discover=lazy_discover, model=model)
migrate = True
# Airpurifiers and Airfresh
elif model in MODELS_PURIFIER_MIOT:
device = AirPurifierMiot(host, token)
device = AirPurifierMiot(host, token, lazy_discover=lazy_discover)
elif model.startswith("zhimi.airpurifier."):
device = AirPurifier(host, token)
device = AirPurifier(host, token, lazy_discover=lazy_discover)
elif model.startswith("zhimi.airfresh."):
device = AirFresh(host, token)
device = AirFresh(host, token, lazy_discover=lazy_discover)
elif model == MODEL_AIRFRESH_A1:
device = AirFreshA1(host, token)
device = AirFreshA1(host, token, lazy_discover=lazy_discover)
elif model == MODEL_AIRFRESH_T2017:
device = AirFreshT2017(host, token)
device = AirFreshT2017(host, token, lazy_discover=lazy_discover)
elif (
model in MODELS_VACUUM
or model.startswith(ROBOROCK_GENERIC)
or model.startswith(ROCKROBO_GENERIC)
):
# TODO: add lazy_discover as argument when python-miio add support # pylint: disable=fixme
device = RoborockVacuum(host, token)
update_method = _async_update_data_vacuum
coordinator_class = DataUpdateCoordinator[VacuumCoordinatorData]
# Pedestal fans
elif model in MODEL_TO_CLASS_MAP:
device = MODEL_TO_CLASS_MAP[model](host, token)
device = MODEL_TO_CLASS_MAP[model](host, token, lazy_discover=lazy_discover)
elif model in MODELS_FAN_MIIO:
device = Fan(host, token, model=model)
device = Fan(host, token, lazy_discover=lazy_discover, model=model)
else:
_LOGGER.error(
"Unsupported device found! Please create an issue at "
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/xiaomi_miio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
CONF_DEVICE,
CONF_FLOW_TYPE,
CONF_GATEWAY,
CONF_LAZY_DISCOVER,
CONF_MAC,
CONF_MANUAL,
DEFAULT_CLOUD_COUNTRY,
Expand Down Expand Up @@ -97,7 +98,11 @@ async def async_step_init(
vol.Optional(
CONF_CLOUD_SUBDEVICES,
default=self.config_entry.options.get(CONF_CLOUD_SUBDEVICES, False),
): bool
): bool,
vol.Optional(
CONF_LAZY_DISCOVER,
default=self.config_entry.options.get(CONF_LAZY_DISCOVER, True),
): bool,
}
)

Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/xiaomi_miio/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# Options flow
CONF_CLOUD_SUBDEVICES = "cloud_subdevices"
CONF_LAZY_DISCOVER = "lazy_discover"

# Keys
KEY_COORDINATOR = "coordinator"
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/xiaomi_miio/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"step": {
"init": {
"data": {
"cloud_subdevices": "Use cloud to get connected subdevices"
"cloud_subdevices": "Use cloud to get connected subdevices",
"lazy_discover": "Use lazy discover to optimize device connectivity"
}
}
}
Expand Down

0 comments on commit 56642b9

Please sign in to comment.