Refactor Livisi Switch and Climate to inherit from a common base class (#89085)

* Refactor Livisi entities to inherit from a common base class

* Add livisi_entity to .coveragerc

* Device location can be None

* Add use_room_as_device_name argument to constructor of LivisiEntity

When initializing, set entity name attribute only if device name differs (i.e. use_room_as_device_name=True).

* re-add comment for special handling of climate device names

* Add explicit type to constructur argument

* Make use_room_as_device_name a keyword only arg

* rename livisi_entity.py to entity.py

* change livisi_entity.py to entity.py in coveragerc

* Code quality improvements as suggested in PR

* sort .coveragerc

* fix isort issue

* fix all isort issues
This commit is contained in:
Felix Rotthowe 2023-03-03 15:23:38 +01:00 committed by GitHub
parent 415190683f
commit 3a34f818e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 147 deletions

View file

@ -8,18 +8,11 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
DOMAIN,
LIVISI_REACHABILITY_CHANGE,
LIVISI_STATE_CHANGE,
LOGGER,
PSS_DEVICE_TYPE,
)
from .const import DOMAIN, LIVISI_STATE_CHANGE, LOGGER, PSS_DEVICE_TYPE
from .coordinator import LivisiDataUpdateCoordinator
from .entity import LivisiEntity
async def async_setup_entry(
@ -40,8 +33,8 @@ async def async_setup_entry(
device["type"] == PSS_DEVICE_TYPE
and device["id"] not in coordinator.devices
):
livisi_switch: SwitchEntity = create_entity(
config_entry, device, coordinator
livisi_switch: SwitchEntity = LivisiSwitch(
config_entry, coordinator, device
)
LOGGER.debug("Include device type: %s", device["type"])
coordinator.devices.add(device["id"])
@ -53,59 +46,18 @@ async def async_setup_entry(
)
def create_entity(
config_entry: ConfigEntry,
device: dict[str, Any],
coordinator: LivisiDataUpdateCoordinator,
) -> SwitchEntity:
"""Create Switch Entity."""
config_details: dict[str, Any] = device["config"]
capabilities: list = device["capabilities"]
room_id: str = device["location"]
room_name: str = coordinator.rooms[room_id]
livisi_switch = LivisiSwitch(
config_entry,
coordinator,
unique_id=device["id"],
manufacturer=device["manufacturer"],
device_type=device["type"],
name=config_details["name"],
capability_id=capabilities[0],
room=room_name,
)
return livisi_switch
class LivisiSwitch(CoordinatorEntity[LivisiDataUpdateCoordinator], SwitchEntity):
class LivisiSwitch(LivisiEntity, SwitchEntity):
"""Represents the Livisi Switch."""
def __init__(
self,
config_entry: ConfigEntry,
coordinator: LivisiDataUpdateCoordinator,
unique_id: str,
manufacturer: str,
device_type: str,
name: str,
capability_id: str,
room: str,
device: dict[str, Any],
) -> None:
"""Initialize the Livisi Switch."""
self.config_entry = config_entry
self._attr_unique_id = unique_id
self._attr_name = name
self._capability_id = capability_id
self.aio_livisi = coordinator.aiolivisi
self._attr_available = False
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
manufacturer=manufacturer,
model=device_type,
name=name,
suggested_area=room,
via_device=(DOMAIN, config_entry.entry_id),
)
super().__init__(coordinator)
"""Initialize the Livisi switch."""
super().__init__(config_entry, coordinator, device)
self._capability_id = self.capabilities["SwitchActuator"]
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
@ -127,6 +79,8 @@ class LivisiSwitch(CoordinatorEntity[LivisiDataUpdateCoordinator], SwitchEntity)
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()
response = await self.coordinator.async_get_pss_state(self._capability_id)
if response is None:
self._attr_is_on = False
@ -140,22 +94,9 @@ class LivisiSwitch(CoordinatorEntity[LivisiDataUpdateCoordinator], SwitchEntity)
self.update_states,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{LIVISI_REACHABILITY_CHANGE}_{self.unique_id}",
self.update_reachability,
)
)
@callback
def update_states(self, state: bool) -> None:
"""Update the states of the switch device."""
"""Update the state of the switch device."""
self._attr_is_on = state
self.async_write_ha_state()
@callback
def update_reachability(self, is_reachable: bool) -> None:
"""Update the reachability of the switch device."""
self._attr_available = is_reachable
self.async_write_ha_state()