mirror of
https://github.com/home-assistant/core.git
synced 2025-09-11 07:41:35 +02:00
Add a base entity to APCUPSD integration (#150828)
This commit is contained in:
@@ -10,9 +10,9 @@ from homeassistant.components.binary_sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .coordinator import APCUPSdConfigEntry, APCUPSdCoordinator
|
from .coordinator import APCUPSdConfigEntry, APCUPSdCoordinator
|
||||||
|
from .entity import APCUPSdEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
@@ -40,22 +40,16 @@ async def async_setup_entry(
|
|||||||
async_add_entities([OnlineStatus(coordinator, _DESCRIPTION)])
|
async_add_entities([OnlineStatus(coordinator, _DESCRIPTION)])
|
||||||
|
|
||||||
|
|
||||||
class OnlineStatus(CoordinatorEntity[APCUPSdCoordinator], BinarySensorEntity):
|
class OnlineStatus(APCUPSdEntity, BinarySensorEntity):
|
||||||
"""Representation of a UPS online status."""
|
"""Representation of a UPS online status."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: APCUPSdCoordinator,
|
coordinator: APCUPSdCoordinator,
|
||||||
description: BinarySensorEntityDescription,
|
description: BinarySensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the APCUPSd binary device."""
|
"""Initialize the APCUPSd binary device."""
|
||||||
super().__init__(coordinator, context=description.key.upper())
|
super().__init__(coordinator, description)
|
||||||
|
|
||||||
self.entity_description = description
|
|
||||||
self._attr_unique_id = f"{coordinator.unique_device_id}_{description.key}"
|
|
||||||
self._attr_device_info = coordinator.device_info
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool | None:
|
||||||
|
26
homeassistant/components/apcupsd/entity.py
Normal file
26
homeassistant/components/apcupsd/entity.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
"""Base entity for APCUPSd integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from .coordinator import APCUPSdCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
class APCUPSdEntity(CoordinatorEntity[APCUPSdCoordinator]):
|
||||||
|
"""Base entity for APCUPSd integration."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: APCUPSdCoordinator,
|
||||||
|
description: EntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the APCUPSd entity."""
|
||||||
|
super().__init__(coordinator, context=description.key.upper())
|
||||||
|
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = f"{coordinator.unique_device_id}_{description.key}"
|
||||||
|
self._attr_device_info = coordinator.device_info
|
@@ -3,10 +3,7 @@ rules:
|
|||||||
action-setup: done
|
action-setup: done
|
||||||
appropriate-polling: done
|
appropriate-polling: done
|
||||||
brands: done
|
brands: done
|
||||||
common-modules:
|
common-modules: done
|
||||||
status: done
|
|
||||||
comment: |
|
|
||||||
Consider deriving a base entity.
|
|
||||||
config-flow-test-coverage: done
|
config-flow-test-coverage: done
|
||||||
config-flow: done
|
config-flow: done
|
||||||
dependency-transparency: done
|
dependency-transparency: done
|
||||||
|
@@ -23,10 +23,10 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import LAST_S_TEST
|
from .const import LAST_S_TEST
|
||||||
from .coordinator import APCUPSdConfigEntry, APCUPSdCoordinator
|
from .coordinator import APCUPSdConfigEntry, APCUPSdCoordinator
|
||||||
|
from .entity import APCUPSdEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
@@ -490,22 +490,16 @@ def infer_unit(value: str) -> tuple[str, str | None]:
|
|||||||
return value, None
|
return value, None
|
||||||
|
|
||||||
|
|
||||||
class APCUPSdSensor(CoordinatorEntity[APCUPSdCoordinator], SensorEntity):
|
class APCUPSdSensor(APCUPSdEntity, SensorEntity):
|
||||||
"""Representation of a sensor entity for APCUPSd status values."""
|
"""Representation of a sensor entity for APCUPSd status values."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: APCUPSdCoordinator,
|
coordinator: APCUPSdCoordinator,
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator=coordinator, context=description.key.upper())
|
super().__init__(coordinator, description)
|
||||||
|
|
||||||
self.entity_description = description
|
|
||||||
self._attr_unique_id = f"{coordinator.unique_device_id}_{description.key}"
|
|
||||||
self._attr_device_info = coordinator.device_info
|
|
||||||
|
|
||||||
# Initial update of attributes.
|
# Initial update of attributes.
|
||||||
self._update_attrs()
|
self._update_attrs()
|
||||||
|
Reference in New Issue
Block a user