1212from homeassistant .helpers .entity import EntityCategory
1313from homeassistant .helpers .entity_platform import AddEntitiesCallback
1414from pymammotion .data .model .hash_list import Plan
15+ from pymammotion .transport .base import TransportType
1516from pymammotion .utility .device_type import DeviceType
1617
1718from . import MammotionConfigEntry
@@ -28,6 +29,7 @@ class MammotionButtonSensorEntityDescription(ButtonEntityDescription):
2829 """Describes Mammotion button sensor entity."""
2930
3031 press_fn : Callable [[MammotionBaseUpdateCoordinator ], Awaitable [None ]]
32+ available_fn : Callable [[MammotionBaseUpdateCoordinator ], bool ] | None = None
3133
3234
3335@dataclass (frozen = True , kw_only = True )
@@ -38,6 +40,17 @@ class MammotionTaskButtonSensorEntityDescription(ButtonEntityDescription):
3840 press_fn : Callable [[MammotionBaseUpdateCoordinator , str ], Awaitable [None ]]
3941
4042
43+ def _nudge_available (coordinator : MammotionBaseUpdateCoordinator ) -> bool :
44+ """Return True when movement via BLE or Wi-Fi is possible."""
45+ if coordinator .config_entry .options .get (CONF_MOVEMENT_USE_WIFI , False ):
46+ return True
47+ handle = coordinator .manager .mower (coordinator .device_name )
48+ if handle is None :
49+ return False
50+ ble = handle .get_transport (TransportType .BLE )
51+ return ble is not None and ble .is_usable
52+
53+
4154BUTTON_SENSORS : tuple [MammotionButtonSensorEntityDescription , ...] = (
4255 MammotionButtonSensorEntityDescription (
4356 key = "start_map_sync" ,
@@ -64,27 +77,31 @@ class MammotionTaskButtonSensorEntityDescription(ButtonEntityDescription):
6477 0.4 ,
6578 coordinator .config_entry .options .get (CONF_MOVEMENT_USE_WIFI , False ),
6679 ),
80+ available_fn = _nudge_available ,
6781 ),
6882 MammotionButtonSensorEntityDescription (
6983 key = "emergency_nudge_left" ,
7084 press_fn = lambda coordinator : coordinator .async_move_left (
7185 0.4 ,
7286 coordinator .config_entry .options .get (CONF_MOVEMENT_USE_WIFI , False ),
7387 ),
88+ available_fn = _nudge_available ,
7489 ),
7590 MammotionButtonSensorEntityDescription (
7691 key = "emergency_nudge_right" ,
7792 press_fn = lambda coordinator : coordinator .async_move_right (
7893 0.4 ,
7994 coordinator .config_entry .options .get (CONF_MOVEMENT_USE_WIFI , False ),
8095 ),
96+ available_fn = _nudge_available ,
8197 ),
8298 MammotionButtonSensorEntityDescription (
8399 key = "emergency_nudge_back" ,
84100 press_fn = lambda coordinator : coordinator .async_move_back (
85101 0.4 ,
86102 coordinator .config_entry .options .get (CONF_MOVEMENT_USE_WIFI , False ),
87103 ),
104+ available_fn = _nudge_available ,
88105 ),
89106 MammotionButtonSensorEntityDescription (
90107 key = "cancel_task" ,
@@ -161,6 +178,15 @@ def __init__(
161178 self .entity_description = entity_description
162179 self ._attr_translation_key = entity_description .key
163180
181+ @property
182+ def available (self ) -> bool :
183+ """Return True if entity is available."""
184+ if self .entity_description .available_fn is not None :
185+ return super ().available and self .entity_description .available_fn (
186+ self .coordinator
187+ )
188+ return super ().available
189+
164190 async def async_press (self ) -> None :
165191 """Handle the button press."""
166192 await self .entity_description .press_fn (self .coordinator )
0 commit comments