1 """Binary sensors for System Monitor."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from functools
import lru_cache
10 from typing
import Literal
12 from psutil
import NoSuchProcess
15 DOMAIN
as BINARY_SENSOR_DOMAIN,
16 BinarySensorDeviceClass,
18 BinarySensorEntityDescription,
27 from .
import SystemMonitorConfigEntry
28 from .const
import CONF_PROCESS, DOMAIN
29 from .coordinator
import SystemMonitorCoordinator
31 _LOGGER = logging.getLogger(__name__)
39 SENSOR_TYPE_DEVICE_CLASS = 3
40 SENSOR_TYPE_MANDATORY_ARG = 4
42 SIGNAL_SYSTEMMONITOR_UPDATE =
"systemmonitor_update"
47 """Return cpu icon."""
48 if sys.maxsize > 2**32:
49 return "mdi:cpu-64-bit"
50 return "mdi:cpu-32-bit"
56 for proc
in entity.coordinator.data.processes:
58 _LOGGER.debug(
"process %s for argument %s", proc.name(), entity.argument)
59 if entity.argument == proc.name():
62 except NoSuchProcess
as err:
64 "Failed to load process with ID: %s, old name: %s",
71 @dataclass(frozen=True, kw_only=True)
73 """Describes System Monitor binary sensor entities."""
75 value_fn: Callable[[SystemMonitorSensor], bool]
76 add_to_update: Callable[[SystemMonitorSensor], tuple[str, str]]
79 SENSOR_TYPES: tuple[SysMonitorBinarySensorEntityDescription, ...] = (
82 translation_key=
"process",
85 device_class=BinarySensorDeviceClass.RUNNING,
86 add_to_update=
lambda entity: (
"processes",
""),
93 entry: SystemMonitorConfigEntry,
94 async_add_entities: AddEntitiesCallback,
96 """Set up System Monitor binary sensors based on a config entry."""
97 coordinator = entry.runtime_data.coordinator
106 for sensor_description
in SENSOR_TYPES
107 for argument
in entry.options.get(BINARY_SENSOR_DOMAIN, {}).
get(
114 CoordinatorEntity[SystemMonitorCoordinator], BinarySensorEntity
116 """Implementation of a system monitor binary sensor."""
118 _attr_has_entity_name =
True
119 _attr_entity_category = EntityCategory.DIAGNOSTIC
120 entity_description: SysMonitorBinarySensorEntityDescription
124 coordinator: SystemMonitorCoordinator,
125 sensor_description: SysMonitorBinarySensorEntityDescription,
129 """Initialize the binary sensor."""
133 self._attr_unique_id: str =
slugify(f
"{sensor_description.key}_{argument}")
135 entry_type=DeviceEntryType.SERVICE,
136 identifiers={(DOMAIN, entry_id)},
137 manufacturer=
"System Monitor",
138 name=
"System Monitor",
143 """When added to hass."""
144 self.coordinator.update_subscribers[
150 """When removed from hass."""
151 self.coordinator.update_subscribers[
158 """Return true if the binary sensor is on."""
None async_will_remove_from_hass(self)
None async_added_to_hass(self)
None __init__(self, SystemMonitorCoordinator coordinator, SysMonitorBinarySensorEntityDescription sensor_description, str entry_id, str argument)
_attr_translation_placeholders
bool add(self, _T matcher)
bool remove(self, _T matcher)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, SystemMonitorConfigEntry entry, AddEntitiesCallback async_add_entities)
bool get_process(SystemMonitorSensor entity)
Literal["mdi:cpu-64-bit", "mdi:cpu-32-bit"] get_cpu_icon()