Skip to content

Commit

Permalink
Introduce base entity in Switcher (#126822)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Sep 27, 2024
1 parent 2b2f5c9 commit d78fcd2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 55 deletions.
12 changes: 2 additions & 10 deletions homeassistant/components/switcher_kis/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager


Expand Down Expand Up @@ -106,13 +104,10 @@ async def async_add_buttons(coordinator: SwitcherDataUpdateCoordinator) -> None:
)


class SwitcherThermostatButtonEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], ButtonEntity
):
class SwitcherThermostatButtonEntity(SwitcherEntity, ButtonEntity):
"""Representation of a Switcher climate entity."""

entity_description: SwitcherThermostatButtonEntityDescription
_attr_has_entity_name = True

def __init__(
self,
Expand All @@ -126,9 +121,6 @@ def __init__(
self._remote = remote

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:
"""Press the button."""
Expand Down
12 changes: 2 additions & 10 deletions homeassistant/components/switcher_kis/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import SwitcherConfigEntry
from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity
from .utils import get_breeze_remote_manager

DEVICE_MODE_TO_HA = {
Expand Down Expand Up @@ -81,12 +79,9 @@ async def async_add_climate(coordinator: SwitcherDataUpdateCoordinator) -> None:
)


class SwitcherClimateEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], ClimateEntity
):
class SwitcherClimateEntity(SwitcherEntity, ClimateEntity):
"""Representation of a Switcher climate entity."""

_attr_has_entity_name = True
_attr_name = None
_enable_turn_on_off_backwards_compatibility = False

Expand All @@ -98,9 +93,6 @@ def __init__(
self._remote = remote

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_max_temp = remote.max_temperature
Expand Down
12 changes: 2 additions & 10 deletions homeassistant/components/switcher_kis/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,12 +51,9 @@ def async_add_cover(coordinator: SwitcherDataUpdateCoordinator) -> None:
)


class SwitcherCoverEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], CoverEntity
):
class SwitcherCoverEntity(SwitcherEntity, CoverEntity):
"""Representation of a Switcher cover entity."""

_attr_has_entity_name = True
_attr_name = None
_attr_device_class = CoverDeviceClass.SHUTTER
_attr_supported_features = (
Expand All @@ -78,9 +73,6 @@ def __init__(
self._cover_id = cover_id

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()

Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/switcher_kis/entity.py
Original file line number Diff line number Diff line change
@@ -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)}
)
13 changes: 2 additions & 11 deletions homeassistant/components/switcher_kis/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfElectricCurrent, UnitOfPower
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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import SIGNAL_DEVICE_ADD
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity

POWER_SENSORS: list[SensorEntityDescription] = [
SensorEntityDescription(
Expand Down Expand Up @@ -79,13 +77,9 @@ def async_add_sensors(coordinator: SwitcherDataUpdateCoordinator) -> None:
)


class SwitcherSensorEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], SensorEntity
):
class SwitcherSensorEntity(SwitcherEntity, SensorEntity):
"""Representation of a Switcher sensor entity."""

_attr_has_entity_name = True

def __init__(
self,
coordinator: SwitcherDataUpdateCoordinator,
Expand All @@ -98,9 +92,6 @@ def __init__(
self._attr_unique_id = (
f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}"
)
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
)

@property
def native_value(self) -> StateType:
Expand Down
17 changes: 3 additions & 14 deletions homeassistant/components/switcher_kis/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_platform,
)
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import VolDictType
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import (
CONF_AUTO_OFF,
Expand All @@ -32,6 +26,7 @@
SIGNAL_DEVICE_ADD,
)
from .coordinator import SwitcherDataUpdateCoordinator
from .entity import SwitcherEntity

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,12 +77,9 @@ def async_add_switch(coordinator: SwitcherDataUpdateCoordinator) -> None:
)


class SwitcherBaseSwitchEntity(
CoordinatorEntity[SwitcherDataUpdateCoordinator], SwitchEntity
):
class SwitcherBaseSwitchEntity(SwitcherEntity, SwitchEntity):
"""Representation of a Switcher switch entity."""

_attr_has_entity_name = True
_attr_name = None

def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:
Expand All @@ -97,9 +89,6 @@ def __init__(self, coordinator: SwitcherDataUpdateCoordinator) -> None:

# Entity class attributes
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
def _handle_coordinator_update(self) -> None:
Expand Down

0 comments on commit d78fcd2

Please sign in to comment.