Skip to content

Commit

Permalink
possibly fix issue around bluetooth and wifi co-existng
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey0000 committed Sep 12, 2024
1 parent fcc978d commit f3513d5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
12 changes: 6 additions & 6 deletions custom_components/mammotion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand Down
7 changes: 4 additions & 3 deletions custom_components/mammotion/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
10 changes: 4 additions & 6 deletions custom_components/mammotion/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mammotion/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
],
"iot_class": "local_push",
"requirements": [
"pymammotion==0.2.32"
"pymammotion==0.2.33"
]
}
57 changes: 51 additions & 6 deletions custom_components/mammotion/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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."""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit f3513d5

Please sign in to comment.