Introduce base entity in Switcher (#126822)

This commit is contained in:
Joost Lekkerkerker 2024-09-27 11:47:47 +02:00 committed by GitHub
parent 2b2f5c9353
commit d78fcd2a29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 55 deletions

View File

@ -20,15 +20,13 @@ from homeassistant.components.button import ButtonEntity, ButtonEntityDescriptio
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import SwitcherConfigEntry from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager from .utils import get_breeze_remote_manager
@ -106,13 +104,10 @@ async def async_setup_entry(
) )
class SwitcherThermostatButtonEntity( class SwitcherThermostatButtonEntity(SwitcherEntity, ButtonEntity):
CoordinatorEntity[SwitcherDataUpdateCoordinator], ButtonEntity
):
"""Representation of a Switcher climate entity.""" """Representation of a Switcher climate entity."""
entity_description: SwitcherThermostatButtonEntityDescription entity_description: SwitcherThermostatButtonEntityDescription
_attr_has_entity_name = True
def __init__( def __init__(
self, self,
@ -126,9 +121,6 @@ class SwitcherThermostatButtonEntity(
self._remote = remote self._remote = remote
self._attr_unique_id = f"{coordinator.mac_address}-{description.key}" self._attr_unique_id = f"{coordinator.mac_address}-{description.key}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)
async def async_press(self) -> None: async def async_press(self) -> None:
"""Press the button.""" """Press the button."""

View File

@ -29,15 +29,13 @@ from homeassistant.components.climate import (
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import SwitcherConfigEntry from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager from .utils import get_breeze_remote_manager
DEVICE_MODE_TO_HA = { DEVICE_MODE_TO_HA = {
@ -81,12 +79,9 @@ async def async_setup_entry(
) )
class SwitcherClimateEntity( class SwitcherClimateEntity(SwitcherEntity, ClimateEntity):
CoordinatorEntity[SwitcherDataUpdateCoordinator], ClimateEntity
):
"""Representation of a Switcher climate entity.""" """Representation of a Switcher climate entity."""
_attr_has_entity_name = True
_attr_name = None _attr_name = None
_enable_turn_on_off_backwards_compatibility = False _enable_turn_on_off_backwards_compatibility = False
@ -98,9 +93,6 @@ class SwitcherClimateEntity(
self._remote = remote self._remote = remote
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}" self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)
self._attr_min_temp = remote.min_temperature self._attr_min_temp = remote.min_temperature
self._attr_max_temp = remote.max_temperature self._attr_max_temp = remote.max_temperature

View File

@ -17,14 +17,12 @@ from homeassistant.components.cover import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import SIGNAL_DEVICE_ADD from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -53,12 +51,9 @@ async def async_setup_entry(
) )
class SwitcherCoverEntity( class SwitcherCoverEntity(SwitcherEntity, CoverEntity):
CoordinatorEntity[SwitcherDataUpdateCoordinator], CoverEntity
):
"""Representation of a Switcher cover entity.""" """Representation of a Switcher cover entity."""
_attr_has_entity_name = True
_attr_name = None _attr_name = None
_attr_device_class = CoverDeviceClass.SHUTTER _attr_device_class = CoverDeviceClass.SHUTTER
_attr_supported_features = ( _attr_supported_features = (
@ -78,9 +73,6 @@ class SwitcherCoverEntity(
self._cover_id = cover_id self._cover_id = cover_id
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}" self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)
self._update_data() self._update_data()

View File

@ -0,0 +1,20 @@
"""Base class for Switcher entities."""
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .coordinator import SwitcherDataUpdateCoordinator
class SwitcherEntity(CoordinatorEntity[SwitcherDataUpdateCoordinator]):
"""Base class for Switcher entities."""
_attr_has_entity_name = True
def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)

View File

@ -13,15 +13,13 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfElectricCurrent, UnitOfPower from homeassistant.const import UnitOfElectricCurrent, UnitOfPower
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import SIGNAL_DEVICE_ADD from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
POWER_SENSORS: list[SensorEntityDescription] = [ POWER_SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription( SensorEntityDescription(
@ -79,13 +77,9 @@ async def async_setup_entry(
) )
class SwitcherSensorEntity( class SwitcherSensorEntity(SwitcherEntity, SensorEntity):
CoordinatorEntity[SwitcherDataUpdateCoordinator], SensorEntity
):
"""Representation of a Switcher sensor entity.""" """Representation of a Switcher sensor entity."""
_attr_has_entity_name = True
def __init__( def __init__(
self, self,
coordinator: SwitcherDataUpdateCoordinator, coordinator: SwitcherDataUpdateCoordinator,
@ -98,9 +92,6 @@ class SwitcherSensorEntity(
self._attr_unique_id = ( self._attr_unique_id = (
f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}" f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}"
) )
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:

View File

@ -13,16 +13,10 @@ import voluptuous as vol
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import ( from homeassistant.helpers import config_validation as cv, entity_platform
config_validation as cv,
device_registry as dr,
entity_platform,
)
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import VolDictType from homeassistant.helpers.typing import VolDictType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ( from .const import (
CONF_AUTO_OFF, CONF_AUTO_OFF,
@ -32,6 +26,7 @@ from .const import (
SIGNAL_DEVICE_ADD, SIGNAL_DEVICE_ADD,
) )
from .coordinator import SwitcherDataUpdateCoordinator from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -82,12 +77,9 @@ async def async_setup_entry(
) )
class SwitcherBaseSwitchEntity( class SwitcherBaseSwitchEntity(SwitcherEntity, SwitchEntity):
CoordinatorEntity[SwitcherDataUpdateCoordinator], SwitchEntity
):
"""Representation of a Switcher switch entity.""" """Representation of a Switcher switch entity."""
_attr_has_entity_name = True
_attr_name = None _attr_name = None
def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None: def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
@ -97,9 +89,6 @@ class SwitcherBaseSwitchEntity(
# Entity class attributes # Entity class attributes
self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}" self._attr_unique_id = f"{coordinator.device_id}-{coordinator.mac_address}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None: