From f3513d5755b6b1c36331d1f637b11092811840b2 Mon Sep 17 00:00:00 2001 From: Michael Arthur Date: Thu, 12 Sep 2024 19:54:08 +1200 Subject: [PATCH] possibly fix issue around bluetooth and wifi co-existng --- custom_components/mammotion/__init__.py | 12 ++--- custom_components/mammotion/config_flow.py | 7 +-- custom_components/mammotion/coordinator.py | 10 ++-- custom_components/mammotion/manifest.json | 2 +- custom_components/mammotion/switch.py | 57 +++++++++++++++++++--- pyproject.toml | 2 +- 6 files changed, 67 insertions(+), 23 deletions(-) diff --git a/custom_components/mammotion/__init__.py b/custom_components/mammotion/__init__.py index 9659d5e..e0d5033 100644 --- a/custom_components/mammotion/__init__.py +++ b/custom_components/mammotion/__init__.py @@ -8,14 +8,14 @@ from homeassistant.helpers import device_registry as dr from .const import ( - CONF_RETRY_COUNT, - DEFAULT_RETRY_COUNT, + CONF_AEP_DATA, CONF_AUTH_DATA, - CONF_USE_WIFI, + CONF_DEVICE_DATA, CONF_REGION_DATA, - CONF_AEP_DATA, + CONF_RETRY_COUNT, CONF_SESSION_DATA, - CONF_DEVICE_DATA, + CONF_USE_WIFI, + DEFAULT_RETRY_COUNT, ) from .coordinator import MammotionDataUpdateCoordinator @@ -54,7 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: MammotionConfigEntry) -> options={CONF_RETRY_COUNT: DEFAULT_RETRY_COUNT}, ) - mammotion_coordinator = MammotionDataUpdateCoordinator(hass) + mammotion_coordinator = MammotionDataUpdateCoordinator(hass, entry) await mammotion_coordinator.async_setup() diff --git a/custom_components/mammotion/config_flow.py b/custom_components/mammotion/config_flow.py index 315bdfb..040ccee 100644 --- a/custom_components/mammotion/config_flow.py +++ b/custom_components/mammotion/config_flow.py @@ -221,9 +221,10 @@ async def async_step_wifi_confirm( password = user_input.get(CONF_PASSWORD) if self._cloud_client is None: - return self.async_abort( - reason="Something went wrong. cloud_client is None." - ) + try: + self._cloud_client = await Mammotion.login(account, password) + except HTTPException as err: + return self.async_abort(reason=str(err)) mowing_devices = self._cloud_client.devices_by_account_response.data.data if name: found_device = [ diff --git a/custom_components/mammotion/coordinator.py b/custom_components/mammotion/coordinator.py index 83038a3..a6647e3 100644 --- a/custom_components/mammotion/coordinator.py +++ b/custom_components/mammotion/coordinator.py @@ -47,14 +47,10 @@ class MammotionDataUpdateCoordinator(DataUpdateCoordinator[MowingDevice]): address: str | None = None config_entry: MammotionConfigEntry - device_name: str = "" manager: Mammotion = None - _operation_settings: OperationSettings = OperationSettings() + _operation_settings: OperationSettings - def __init__( - self, - hass: HomeAssistant, - ) -> None: + def __init__(self, hass: HomeAssistant, config_entry: MammotionConfigEntry) -> None: """Initialize global mammotion data updater.""" super().__init__( hass=hass, @@ -63,6 +59,8 @@ def __init__( update_interval=UPDATE_INTERVAL, ) assert self.config_entry.unique_id + self.config_entry = config_entry + self._operation_settings = OperationSettings() self.update_failures = 0 async def async_setup(self) -> None: diff --git a/custom_components/mammotion/manifest.json b/custom_components/mammotion/manifest.json index 3e86fde..13475ff 100644 --- a/custom_components/mammotion/manifest.json +++ b/custom_components/mammotion/manifest.json @@ -30,6 +30,6 @@ ], "iot_class": "local_push", "requirements": [ - "pymammotion==0.2.32" + "pymammotion==0.2.33" ] } diff --git a/custom_components/mammotion/switch.py b/custom_components/mammotion/switch.py index 96cef42..61ba382 100644 --- a/custom_components/mammotion/switch.py +++ b/custom_components/mammotion/switch.py @@ -29,6 +29,15 @@ class MammotionConfigSwitchEntityDescription(SwitchEntityDescription): set_fn: Callable[[MammotionDataUpdateCoordinator, bool], None] +@dataclass(frozen=True, kw_only=True) +class MammotionConfigAreaSwitchEntityDescription(SwitchEntityDescription): + """Describes the Areas entities.""" + + key: str + area: int + set_fn: Callable[[MammotionDataUpdateCoordinator, bool, int], None] + + YUKA_CONFIG_SWITCH_ENTITIES: tuple[MammotionConfigSwitchEntityDescription, ...] = ( MammotionConfigSwitchEntityDescription( key="mowing_on_off", @@ -93,17 +102,18 @@ def add_entities() -> None: (area for area in area_name if area.hash == area_id), None ) name = existing_name.name if existing_name else area_id - base_area_switch_entity = MammotionConfigSwitchEntityDescription( + base_area_switch_entity = MammotionConfigAreaSwitchEntityDescription( key=f"{area_id}", + area=area_id, name=f"{name}", - set_fn=lambda coord, value: coord.operation_settings.areas.append( - value - ) - if value + set_fn=lambda coord, + bool_val, + value: coord.operation_settings.areas.append(value) + if bool_val else coord.operation_settings.areas.remove(value), ) switch_entities.append( - MammotionConfigSwitchEntity( + MammotionConfigAreaSwitchEntity( coordinator, base_area_switch_entity, ) @@ -189,3 +199,38 @@ async def async_turn_off(self, **kwargs: Any) -> None: async def async_update(self) -> None: """Update the entity state.""" + + +class MammotionConfigAreaSwitchEntity(MammotionBaseEntity, SwitchEntity, RestoreEntity): + entity_description: MammotionConfigAreaSwitchEntityDescription + _attr_has_entity_name = True + _attr_entity_category = EntityCategory.CONFIG + + def __init__( + self, + coordinator: MammotionDataUpdateCoordinator, + entity_description: MammotionConfigAreaSwitchEntityDescription, + ) -> None: + super().__init__(coordinator, entity_description.key) + self.coordinator = coordinator + self.entity_description = entity_description + self._attr_translation_key = entity_description.key + # TODO grab defaults from operation_settings + self._attr_is_on = False # Default state + + async def async_turn_on(self, **kwargs: Any) -> None: + self._attr_is_on = True + self.entity_description.set_fn( + self.coordinator, True, self.entity_description.area + ) + self.async_write_ha_state() + + async def async_turn_off(self, **kwargs: Any) -> None: + self._attr_is_on = False + self.entity_description.set_fn( + self.coordinator, False, self.entity_description.area + ) + self.async_write_ha_state() + + async def async_update(self) -> None: + """Update the entity state.""" diff --git a/pyproject.toml b/pyproject.toml index 8e9bd27..d3b16a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ readme = "README.md" [tool.poetry.dependencies] python = "~3.12.0" -pymammotion = "0.2.32" +pymammotion = "0.2.33" homeassistant = "^2024.7.4" autotyping = "^24.3.0"