Remove duplicate code in livisi coordinator (#90227)

* Simplify coordinator

* remove window sensor specific code (isOpen)

* parameter order, type hinta

* Update homeassistant/components/livisi/coordinator.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/livisi/coordinator.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Felix Rotthowe 2023-03-24 12:59:59 +01:00 committed by GitHub
parent ee74e21541
commit 3157579992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 65 deletions

View File

@ -99,14 +99,15 @@ class LivisiClimate(LivisiEntity, ClimateEntity):
await super().async_added_to_hass() await super().async_added_to_hass()
target_temperature = await self.coordinator.async_get_vrcc_target_temperature( target_temperature = await self.coordinator.async_get_device_state(
self._target_temperature_capability self._target_temperature_capability,
"setpointTemperature" if self.coordinator.is_avatar else "pointTemperature",
) )
temperature = await self.coordinator.async_get_vrcc_temperature( temperature = await self.coordinator.async_get_device_state(
self._temperature_capability self._temperature_capability, "temperature"
) )
humidity = await self.coordinator.async_get_vrcc_humidity( humidity = await self.coordinator.async_get_device_state(
self._humidity_capability self._humidity_capability, "humidity"
) )
if temperature is None: if temperature is None:
self._attr_current_temperature = None self._attr_current_temperature = None

View File

@ -58,6 +58,10 @@ class LivisiDataUpdateCoordinator(DataUpdateCoordinator[list[dict[str, Any]]]):
except ClientConnectorError as exc: except ClientConnectorError as exc:
raise UpdateFailed("Failed to get LIVISI the devices") from exc raise UpdateFailed("Failed to get LIVISI the devices") from exc
def _async_dispatcher_send(self, event: str, source: str, data: Any) -> None:
if data is not None:
async_dispatcher_send(self.hass, f"{event}_{source}", data)
async def async_setup(self) -> None: async def async_setup(self) -> None:
"""Set up the Livisi Smart Home Controller.""" """Set up the Livisi Smart Home Controller."""
if not self.aiolivisi.livisi_connection_data: if not self.aiolivisi.livisi_connection_data:
@ -83,44 +87,14 @@ class LivisiDataUpdateCoordinator(DataUpdateCoordinator[list[dict[str, Any]]]):
"""Set the discovered devices list.""" """Set the discovered devices list."""
return await self.aiolivisi.async_get_devices() return await self.aiolivisi.async_get_devices()
async def async_get_pss_state(self, capability: str) -> bool | None: async def async_get_device_state(self, capability: str, key: str) -> Any | None:
"""Set the PSS state.""" """Get state from livisi devices."""
response: dict[str, Any] | None = await self.aiolivisi.async_get_device_state( response: dict[str, Any] = await self.aiolivisi.async_get_device_state(
capability[1:] capability[1:]
) )
if response is None: if response is None:
return None return None
on_state = response["onState"] return response.get(key, {}).get("value")
return on_state["value"]
async def async_get_vrcc_target_temperature(self, capability: str) -> float | None:
"""Get the target temperature of the climate device."""
response: dict[str, Any] | None = await self.aiolivisi.async_get_device_state(
capability[1:]
)
if response is None:
return None
if self.is_avatar:
return response["setpointTemperature"]["value"]
return response["pointTemperature"]["value"]
async def async_get_vrcc_temperature(self, capability: str) -> float | None:
"""Get the temperature of the climate device."""
response: dict[str, Any] | None = await self.aiolivisi.async_get_device_state(
capability[1:]
)
if response is None:
return None
return response["temperature"]["value"]
async def async_get_vrcc_humidity(self, capability: str) -> int | None:
"""Get the humidity of the climate device."""
response: dict[str, Any] | None = await self.aiolivisi.async_get_device_state(
capability[1:]
)
if response is None:
return None
return response["humidity"]["value"]
async def async_set_all_rooms(self) -> None: async def async_set_all_rooms(self) -> None:
"""Set the room list.""" """Set the room list."""
@ -132,34 +106,20 @@ class LivisiDataUpdateCoordinator(DataUpdateCoordinator[list[dict[str, Any]]]):
def on_data(self, event_data: LivisiEvent) -> None: def on_data(self, event_data: LivisiEvent) -> None:
"""Define a handler to fire when the data is received.""" """Define a handler to fire when the data is received."""
if event_data.onState is not None: self._async_dispatcher_send(
async_dispatcher_send( LIVISI_STATE_CHANGE, event_data.source, event_data.onState
self.hass, )
f"{LIVISI_STATE_CHANGE}_{event_data.source}", self._async_dispatcher_send(
event_data.onState, LIVISI_STATE_CHANGE, event_data.source, event_data.vrccData
) )
if event_data.vrccData is not None: self._async_dispatcher_send(
async_dispatcher_send( LIVISI_REACHABILITY_CHANGE, event_data.source, event_data.isReachable
self.hass, )
f"{LIVISI_STATE_CHANGE}_{event_data.source}",
event_data.vrccData,
)
if event_data.isReachable is not None:
async_dispatcher_send(
self.hass,
f"{LIVISI_REACHABILITY_CHANGE}_{event_data.source}",
event_data.isReachable,
)
async def on_close(self) -> None: async def on_close(self) -> None:
"""Define a handler to fire when the websocket is closed.""" """Define a handler to fire when the websocket is closed."""
for device_id in self.devices: for device_id in self.devices:
is_reachable: bool = False self._async_dispatcher_send(LIVISI_REACHABILITY_CHANGE, device_id, False)
async_dispatcher_send(
self.hass,
f"{LIVISI_REACHABILITY_CHANGE}_{device_id}",
is_reachable,
)
await self.websocket.connect(self.on_data, self.on_close, self.port) await self.websocket.connect(self.on_data, self.on_close, self.port)

View File

@ -81,7 +81,9 @@ class LivisiSwitch(LivisiEntity, SwitchEntity):
"""Register callbacks.""" """Register callbacks."""
await super().async_added_to_hass() await super().async_added_to_hass()
response = await self.coordinator.async_get_pss_state(self._capability_id) response = await self.coordinator.async_get_device_state(
self._capability_id, "onState"
)
if response is None: if response is None:
self._attr_is_on = False self._attr_is_on = False
self._attr_available = False self._attr_available = False