44
55from collections .abc import Callable
66from dataclasses import dataclass
7- from typing import Any
8-
9- from pyportainer .models .docker import DockerContainer
107
118from homeassistant .components .binary_sensor import (
129 BinarySensorDeviceClass ,
1815from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
1916
2017from . import PortainerConfigEntry
21- from .coordinator import PortainerCoordinator
18+ from .coordinator import PortainerContainerData , PortainerCoordinator
2219from .entity import (
2320 PortainerContainerEntity ,
2421 PortainerCoordinatorData ,
2724
2825
2926@dataclass (frozen = True , kw_only = True )
30- class PortainerBinarySensorEntityDescription (BinarySensorEntityDescription ):
31- """Class to hold Portainer binary sensor description."""
27+ class PortainerContainerBinarySensorEntityDescription (BinarySensorEntityDescription ):
28+ """Class to hold Portainer container binary sensor description."""
29+
30+ state_fn : Callable [[PortainerContainerData ], bool | None ]
31+
32+
33+ @dataclass (frozen = True , kw_only = True )
34+ class PortainerEndpointBinarySensorEntityDescription (BinarySensorEntityDescription ):
35+ """Class to hold Portainer endpoint binary sensor description."""
3236
33- state_fn : Callable [[Any ], bool ]
37+ state_fn : Callable [[PortainerCoordinatorData ], bool | None ]
3438
3539
36- CONTAINER_SENSORS : tuple [PortainerBinarySensorEntityDescription , ...] = (
37- PortainerBinarySensorEntityDescription (
40+ CONTAINER_SENSORS : tuple [PortainerContainerBinarySensorEntityDescription , ...] = (
41+ PortainerContainerBinarySensorEntityDescription (
3842 key = "status" ,
3943 translation_key = "status" ,
40- state_fn = lambda data : data .state == "running" ,
44+ state_fn = lambda data : data .container . state == "running" ,
4145 device_class = BinarySensorDeviceClass .RUNNING ,
4246 entity_category = EntityCategory .DIAGNOSTIC ,
4347 ),
4448)
4549
46- ENDPOINT_SENSORS : tuple [PortainerBinarySensorEntityDescription , ...] = (
47- PortainerBinarySensorEntityDescription (
50+ ENDPOINT_SENSORS : tuple [PortainerEndpointBinarySensorEntityDescription , ...] = (
51+ PortainerEndpointBinarySensorEntityDescription (
4852 key = "status" ,
4953 translation_key = "status" ,
5054 state_fn = lambda data : data .endpoint .status == 1 , # 1 = Running | 2 = Stopped
@@ -76,7 +80,7 @@ def _async_add_new_endpoints(endpoints: list[PortainerCoordinatorData]) -> None:
7680 )
7781
7882 def _async_add_new_containers (
79- containers : list [tuple [PortainerCoordinatorData , DockerContainer ]],
83+ containers : list [tuple [PortainerCoordinatorData , PortainerContainerData ]],
8084 ) -> None :
8185 """Add new container binary sensors."""
8286 async_add_entities (
@@ -113,12 +117,12 @@ def _async_add_new_containers(
113117class PortainerEndpointSensor (PortainerEndpointEntity , BinarySensorEntity ):
114118 """Representation of a Portainer endpoint binary sensor entity."""
115119
116- entity_description : PortainerBinarySensorEntityDescription
120+ entity_description : PortainerEndpointBinarySensorEntityDescription
117121
118122 def __init__ (
119123 self ,
120124 coordinator : PortainerCoordinator ,
121- entity_description : PortainerBinarySensorEntityDescription ,
125+ entity_description : PortainerEndpointBinarySensorEntityDescription ,
122126 device_info : PortainerCoordinatorData ,
123127 ) -> None :
124128 """Initialize Portainer endpoint binary sensor entity."""
@@ -141,13 +145,13 @@ def is_on(self) -> bool | None:
141145class PortainerContainerSensor (PortainerContainerEntity , BinarySensorEntity ):
142146 """Representation of a Portainer container sensor."""
143147
144- entity_description : PortainerBinarySensorEntityDescription
148+ entity_description : PortainerContainerBinarySensorEntityDescription
145149
146150 def __init__ (
147151 self ,
148152 coordinator : PortainerCoordinator ,
149- entity_description : PortainerBinarySensorEntityDescription ,
150- device_info : DockerContainer ,
153+ entity_description : PortainerContainerBinarySensorEntityDescription ,
154+ device_info : PortainerContainerData ,
151155 via_device : PortainerCoordinatorData ,
152156 ) -> None :
153157 """Initialize the Portainer container sensor."""
@@ -164,6 +168,4 @@ def available(self) -> bool:
164168 @property
165169 def is_on (self ) -> bool | None :
166170 """Return true if the binary sensor is on."""
167- return self .entity_description .state_fn (
168- self .coordinator .data [self .endpoint_id ].containers [self .device_name ]
169- )
171+ return self .entity_description .state_fn (self .container_data )
0 commit comments