|
25 | 25 | from homeassistant.helpers.device_registry import DeviceInfo |
26 | 26 |
|
27 | 27 | from . import _LOGGER |
28 | | -from .const import CONF_DRIVES, CONF_SHARES, CONF_VMS, DOMAIN |
| 28 | +from .const import CONF_DOCKER_MODE, CONF_DRIVES, CONF_SHARES, CONF_VMS, DOCKER_MODE_OFF, DOMAIN |
29 | 29 | from .entity import UnraidBaseEntity, UnraidEntityDescription |
30 | | -from .models import Disk, DiskType, DockerContainer, Share, UpsDevice, VirtualMachine, VmState |
| 30 | +from .models import ( |
| 31 | + ContainerState, |
| 32 | + Disk, |
| 33 | + DiskType, |
| 34 | + DockerContainer, |
| 35 | + Share, |
| 36 | + UpsDevice, |
| 37 | + VirtualMachine, |
| 38 | + VmState, |
| 39 | +) |
31 | 40 |
|
32 | 41 | if TYPE_CHECKING: |
33 | 42 | from collections.abc import Callable |
@@ -406,6 +415,35 @@ def calc_disk_usage_percentage(disk: Disk) -> StateType: |
406 | 415 | ), |
407 | 416 | ) |
408 | 417 |
|
| 418 | +DOCKER_AGGREGATE_SENSOR_DESCRIPTIONS: tuple[UnraidSensorEntityDescription, ...] = ( |
| 419 | + UnraidSensorEntityDescription( |
| 420 | + key="docker_running", |
| 421 | + state_class=SensorStateClass.MEASUREMENT, |
| 422 | + value_fn=lambda coordinator: sum( |
| 423 | + 1 |
| 424 | + for c in coordinator.data["docker_containers"].values() |
| 425 | + if c.state == ContainerState.RUNNING |
| 426 | + ), |
| 427 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 428 | + ), |
| 429 | + UnraidSensorEntityDescription( |
| 430 | + key="docker_exited", |
| 431 | + state_class=SensorStateClass.MEASUREMENT, |
| 432 | + value_fn=lambda coordinator: sum( |
| 433 | + 1 |
| 434 | + for c in coordinator.data["docker_containers"].values() |
| 435 | + if c.state == ContainerState.EXITED |
| 436 | + ), |
| 437 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 438 | + ), |
| 439 | + UnraidSensorEntityDescription( |
| 440 | + key="docker_total", |
| 441 | + state_class=SensorStateClass.MEASUREMENT, |
| 442 | + value_fn=lambda coordinator: len(coordinator.data["docker_containers"]), |
| 443 | + entity_category=EntityCategory.DIAGNOSTIC, |
| 444 | + ), |
| 445 | +) |
| 446 | + |
409 | 447 | VM_AGGREGATE_SENSOR_DESCRIPTIONS: tuple[UnraidSensorEntityDescription, ...] = ( |
410 | 448 | UnraidSensorEntityDescription( |
411 | 449 | key="vms_running", |
@@ -435,6 +473,12 @@ async def async_setup_entry( |
435 | 473 | for description in SENSOR_DESCRIPTIONS |
436 | 474 | if description.min_version <= config_entry.runtime_data.coordinator.api_client.version |
437 | 475 | ] |
| 476 | + if config_entry.options.get(CONF_DOCKER_MODE, DOCKER_MODE_OFF) != DOCKER_MODE_OFF: |
| 477 | + entities.extend( |
| 478 | + UnraidSensor(description, config_entry) |
| 479 | + for description in DOCKER_AGGREGATE_SENSOR_DESCRIPTIONS |
| 480 | + if description.min_version <= config_entry.runtime_data.coordinator.api_client.version |
| 481 | + ) |
438 | 482 | if config_entry.options.get(CONF_VMS): |
439 | 483 | entities.extend( |
440 | 484 | UnraidSensor(description, config_entry) |
|
0 commit comments