Coverage for custom_components/ha_repl_server/button.py: 84%

28 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-17 19:05 +0000

1"""A Stop button, on the integration's own device, stopping the code running 

2in every live session - from Home Assistant itself, a dashboard or an 

3automation, when the shell that started it can't (gone, or stuck waiting).""" 

4 

5from __future__ import annotations 

6 

7from homeassistant.components.button import ButtonEntity 

8from homeassistant.config_entries import ConfigEntry 

9from homeassistant.core import HomeAssistant 

10from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo 

11from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback 

12 

13from .const import DOMAIN 

14from .session import SessionManager 

15from .websocket_api import COMMAND_LOG 

16 

17# Pressing only cancels tasks on the event loop: nothing to limit. 

18PARALLEL_UPDATES = 0 

19 

20 

21async def async_setup_entry( 

22 hass: HomeAssistant, 

23 entry: ConfigEntry, 

24 async_add_entities: AddConfigEntryEntitiesCallback, 

25) -> None: 

26 async_add_entities([StopButton(entry)]) 

27 

28 

29class StopButton(ButtonEntity): 

30 _attr_has_entity_name = True 

31 _attr_translation_key = "stop" 

32 

33 def __init__(self, entry: ConfigEntry) -> None: 

34 self._attr_unique_id = f"{entry.entry_id}_stop" 

35 self._attr_device_info = DeviceInfo( 

36 identifiers={(DOMAIN, entry.entry_id)}, 

37 name=entry.title, 

38 entry_type=DeviceEntryType.SERVICE, 

39 ) 

40 

41 async def async_press(self) -> None: 

42 manager: SessionManager | None = self.hass.data.get(DOMAIN) 

43 if manager is None: 43 ↛ 44line 43 didn't jump to line 44 because the condition on line 43 was never true

44 return 

45 who = "Home Assistant" 

46 if self._context and self._context.user_id: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true

47 user = await self.hass.auth.async_get_user(self._context.user_id) 

48 who = user.name or user.id if user else who 

49 stopped = manager.cancel(None, f"Stopped by {who}, from the Stop button") 

50 COMMAND_LOG.warning( 

51 "%s stopped %s from the Stop button", 

52 who, 

53 ", ".join(f"session {name!r}" for name in stopped) 

54 or "nothing (none running)", 

55 )