Skip to content

Commit

Permalink
Merge pull request #207 from mikey0000/feature/area-names
Browse files Browse the repository at this point in the history
Area switch names
  • Loading branch information
mikey0000 authored Nov 14, 2024
2 parents c9bb637 + 549bfd5 commit 35f81c9
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pytest
.pytest_cache
.cache
.venv

# GITHUB Proposed Python stuff:
*.py[cod]
Expand Down
13 changes: 2 additions & 11 deletions custom_components/mammotion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@
from homeassistant.helpers import device_registry as dr

from .const import (
CONF_ACCOUNTNAME,
CONF_AEP_DATA,
CONF_AUTH_DATA,
CONF_CONNECT_DATA,
CONF_DEVICE_DATA,
CONF_REGION_DATA,
CONF_RETRY_COUNT,
CONF_SESSION_DATA,
CONF_USE_WIFI,
DEFAULT_RETRY_COUNT,
DOMAIN,
)
from .coordinator import MammotionDataUpdateCoordinator

Expand Down Expand Up @@ -74,7 +65,7 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:

if unload_ok:= await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await entry.runtime_data.manager.remove_device(entry.runtime_data.device_name)
return unload_ok
7 changes: 1 addition & 6 deletions custom_components/mammotion/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MammotionTracker(MammotionBaseEntity, TrackerEntity, RestoreEntity):

_attr_force_update = False
_attr_translation_key = "device_tracker"
_attr_icon = "mdi:robot-mower"
_attr_source_type = SourceType.GPS

def __init__(self, coordinator: MammotionDataUpdateCoordinator) -> None:
"""Initialize the Tracker."""
Expand Down Expand Up @@ -67,8 +67,3 @@ def longitude(self) -> float | None:
def battery_level(self) -> int | None:
"""Return the battery level of the device."""
return self.coordinator.data.report_data.dev.battery_val

@property
def source_type(self) -> SourceType:
"""Return the source type, e.g., GPS or router, of the device."""
return SourceType.GPS
71 changes: 71 additions & 0 deletions custom_components/mammotion/icons.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
{
"entity": {
"device_tracker": {
"device_tracker": {
"default": "mdi:map-marker-radius"
}
},
"button": {
"start_map_sync": {
"default": "mdi:map-clock"
},
"resync_rtk_dock": {
"default": "mdi:sync"
},
"release_from_dock": {
"default": "mdi:ray-start-arrow"
},
"emergency_nudge_forward": {
"default": "mdi:arrow-up"
},
"emergency_nudge_left": {
"default": "mdi:arrow-left"
},
"emergency_nudge_right": {
"default": "mdi:arrow-right"
},
"emergency_nudge_back": {
"default": "mdi:arrow-down"
},
"cancel_task": {
"default": "mdi:cancel"
},
"clear_all_mapdata": {
"default": "mdi:map-marker-remove"
}
},
"sensor": {
"gps_stars": {
"default": "mdi:satellite-uplink"
Expand All @@ -22,6 +56,43 @@
"position_mode": {
"default": "mdi:map-marker"
}
},
"switch": {
"area": {
"default": "mdi:texture-box"
},
"blade_status": {
"default": "mdi:saw-blade"
},
"is_mow": {
"default": "mdi:mower",
"state": {
"off": "mdi:mower",
"on": "mdi:mower-on"
}
},
"is_dump": {
"default": "mdi:dump-truck"
},
"is_edge": {
"default": "mdi:border-outside"
},
"rain_tactics": {
"default": "mdi:weather-rainy"
},
"side_led": {
"default": "mdi:led-off",
"state": {
"off": "mdi:led-off",
"on": "mdi:led-on"
}
},
"perimeter_first_on_off": {
"default": "mdi:dots-square"
},
"schedule_updates": {
"default": "mdi:update"
}
}
}
}
5 changes: 4 additions & 1 deletion custom_components/mammotion/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
}
},
"switch": {
"area": {
"name": "Area {name}"
},
"blade_status": {
"name": "Blades on/off"
},
Expand Down Expand Up @@ -362,4 +365,4 @@
"message": "Failed to send command to the mower."
}
}
}
}
55 changes: 27 additions & 28 deletions custom_components/mammotion/switch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Support for Mammotion switches."""

from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any, Awaitable, Callable, cast
from typing import Any

from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant, callback
Expand All @@ -18,32 +21,34 @@ class MammotionSwitchEntityDescription(SwitchEntityDescription):
"""Describes Mammotion switch entity."""

key: str
set_fn: Callable[[MammotionDataUpdateCoordinator, bool], Awaitable[None]]


@dataclass(frozen=True, kw_only=True)
class MammotionUpdateSwitchEntityDescription(SwitchEntityDescription):
class MammotionAsyncSwitchEntityDescription(MammotionSwitchEntityDescription):
"""Describes Mammotion switch entity."""

key: str
set_fn: Callable[[MammotionDataUpdateCoordinator, bool], Awaitable[None]]


@dataclass(frozen=True, kw_only=True)
class MammotionUpdateSwitchEntityDescription(MammotionAsyncSwitchEntityDescription):
"""Describes Mammotion switch entity."""

is_on_func: Callable[[MammotionDataUpdateCoordinator], bool]


@dataclass(frozen=True, kw_only=True)
class MammotionConfigSwitchEntityDescription(SwitchEntityDescription):
class MammotionConfigSwitchEntityDescription(MammotionSwitchEntityDescription):
"""Describes Mammotion Config switch entity."""

key: str
set_fn: Callable[[MammotionDataUpdateCoordinator, bool], None]


@dataclass(frozen=True, kw_only=True)
class MammotionConfigAreaSwitchEntityDescription(SwitchEntityDescription):
class MammotionConfigAreaSwitchEntityDescription(MammotionSwitchEntityDescription):
"""Describes the Areas entities."""

key: str
area: int
area: str
set_fn: Callable[[MammotionDataUpdateCoordinator, bool, int], None]


Expand All @@ -68,12 +73,12 @@ class MammotionConfigAreaSwitchEntityDescription(SwitchEntityDescription):
),
)

SWITCH_ENTITIES: tuple[MammotionSwitchEntityDescription, ...] = (
MammotionSwitchEntityDescription(
SWITCH_ENTITIES: tuple[MammotionAsyncSwitchEntityDescription, ...] = (
MammotionAsyncSwitchEntityDescription(
key="blade_status",
set_fn=lambda coordinator, value: coordinator.async_start_stop_blades(value),
),
MammotionSwitchEntityDescription(
MammotionAsyncSwitchEntityDescription(
key="side_led",
set_fn=lambda coordinator, value: coordinator.async_set_sidelight(int(value)),
),
Expand All @@ -91,7 +96,7 @@ class MammotionConfigAreaSwitchEntityDescription(SwitchEntityDescription):
MammotionConfigSwitchEntityDescription(
key="rain_tactics",
set_fn=lambda coordinator, value: setattr(
coordinator.operation_settings, "rain_tactics", cast(value, int)
coordinator.operation_settings, "rain_tactics", int(value)
),
),
)
Expand All @@ -116,23 +121,21 @@ def add_entities() -> None:
new_areas = (set(areas) | set(area_name_hashes)) - added_areas
if new_areas:
for area_id in new_areas:
existing_name: AreaHashNameList = next(
existing_name: AreaHashNameList | None = next(
(area for area in area_name if str(area.hash) == str(area_id)), None
)
name = (
existing_name.name
if (existing_name is None or existing_name != "")
else f"Area {area_id}"
)
name = existing_name.name if (existing_name and existing_name.name) else f"{area_id}"
base_area_switch_entity = MammotionConfigAreaSwitchEntityDescription(
key=f"{area_id}",
translation_key="area",
translation_placeholders={"name": name},
area=area_id,
name=f"{name}",
set_fn=lambda coord,
bool_val,
value: coord.operation_settings.areas.append(value)
if bool_val
else coord.operation_settings.areas.remove(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(
MammotionConfigAreaSwitchEntity(
Expand Down Expand Up @@ -276,7 +279,6 @@ def __init__(
super().__init__(coordinator, entity_description.key)
self.coordinator = coordinator
self.entity_description = entity_description
self._attr_translation_key = entity_description.key
# TODO this should not need to be cast.
self._attr_extra_state_attributes = {"hash": entity_description.area}
# TODO grab defaults from operation_settings
Expand All @@ -301,6 +303,3 @@ async def async_turn_off(self, **kwargs: Any) -> None:
int(self.entity_description.area),
)
self.async_write_ha_state()

async def async_update(self) -> None:
"""Update the entity state."""
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Oblast {name}"
},
"blade_status": {
"name": "Zapnutí/vypnutí nožů"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Bereich {name}"
},
"blade_status": {
"name": "Klingen An/Aus"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Area {name}"
},
"blade_status": {
"name": "Blades on/off"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Zone {name}"
},
"blade_status": {
"name": "Lames On/Off"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Zona {name}"
},
"blade_status": {
"name": "Lame On/Off"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Zones {name}"
},
"blade_status": {
"name": "Maai Messen aan/uit"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Obszar {name}"
},
"blade_status": {
"name": "Ostrza wł/wył"
},
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/translations/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
}
},
"switch": {
"area": {
"name": "Suprafață {name}"
},
"blade_status": {
"name": "Lame pornite/oprite"
},
Expand Down

0 comments on commit 35f81c9

Please sign in to comment.