Simplify coordinator and import OverkizStateType (#63969)
This commit is contained in:
parent
090a9f94f0
commit
1cf2f0b944
8 changed files with 17 additions and 42 deletions
|
@ -5,6 +5,7 @@ from collections.abc import Callable
|
|||
from dataclasses import dataclass
|
||||
|
||||
from pyoverkiz.enums import OverkizCommandParam, OverkizState
|
||||
from pyoverkiz.types import StateType as OverkizStateType
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
|
@ -16,7 +17,7 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import HomeAssistantOverkizData
|
||||
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES, OverkizStateType
|
||||
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
|
||||
from .entity import OverkizDescriptiveEntity
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Any, Final, Union
|
||||
from typing import Final
|
||||
|
||||
from pyoverkiz.enums import UIClass
|
||||
from pyoverkiz.enums.ui import UIWidget
|
||||
|
@ -37,5 +37,3 @@ OVERKIZ_DEVICE_TO_PLATFORM: dict[UIClass | UIWidget, Platform] = {
|
|||
UIClass.DOOR_LOCK: Platform.LOCK,
|
||||
UIClass.LIGHT: Platform.LIGHT,
|
||||
}
|
||||
|
||||
OverkizStateType = Union[str, int, float, bool, dict[Any, Any], list[Any], None]
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
"""Helpers to help coordinate updates."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from datetime import timedelta
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import ServerDisconnectedError
|
||||
from pyoverkiz.client import OverkizClient
|
||||
|
@ -16,23 +13,13 @@ from pyoverkiz.exceptions import (
|
|||
NotAuthenticatedException,
|
||||
TooManyRequestsException,
|
||||
)
|
||||
from pyoverkiz.models import DataType, Device, Place, State
|
||||
from pyoverkiz.models import Device, Place
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN, UPDATE_INTERVAL, OverkizStateType
|
||||
|
||||
DATA_TYPE_TO_PYTHON: dict[DataType, Callable[[Any], OverkizStateType]] = {
|
||||
DataType.INTEGER: int,
|
||||
DataType.DATE: int,
|
||||
DataType.STRING: str,
|
||||
DataType.FLOAT: float,
|
||||
DataType.BOOLEAN: bool,
|
||||
DataType.JSON_ARRAY: json.loads,
|
||||
DataType.JSON_OBJECT: json.loads,
|
||||
}
|
||||
from .const import DOMAIN, UPDATE_INTERVAL
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -77,7 +64,7 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||
try:
|
||||
events = await self.client.fetch_events()
|
||||
except BadCredentialsException as exception:
|
||||
raise UpdateFailed("Invalid authentication") from exception
|
||||
raise UpdateFailed("Invalid authentication.") from exception
|
||||
except TooManyRequestsException as exception:
|
||||
raise UpdateFailed("Too many requests, try again later.") from exception
|
||||
except MaintenanceException as exception:
|
||||
|
@ -92,7 +79,7 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||
await self.client.login()
|
||||
self.devices = await self._get_devices()
|
||||
except BadCredentialsException as exception:
|
||||
raise UpdateFailed("Invalid authentication") from exception
|
||||
raise UpdateFailed("Invalid authentication.") from exception
|
||||
except TooManyRequestsException as exception:
|
||||
raise UpdateFailed("Too many requests, try again later.") from exception
|
||||
|
||||
|
@ -137,7 +124,7 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||
device_state = state
|
||||
device.states[state.name] = device_state
|
||||
|
||||
device_state.value = self._get_state(state)
|
||||
device_state.value = state.value
|
||||
|
||||
elif event.name == EventName.EXECUTION_REGISTERED:
|
||||
if event.exec_id not in self.executions:
|
||||
|
@ -163,21 +150,6 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||
_LOGGER.debug("Fetching all devices and state via /setup/devices")
|
||||
return {d.device_url: d for d in await self.client.get_devices(refresh=True)}
|
||||
|
||||
@staticmethod
|
||||
def _get_state(
|
||||
state: State,
|
||||
) -> OverkizStateType:
|
||||
"""Cast string value to the right type."""
|
||||
data_type = DataType(state.type)
|
||||
|
||||
if data_type == DataType.NONE:
|
||||
return state.value
|
||||
|
||||
cast_to_python = DATA_TYPE_TO_PYTHON[data_type]
|
||||
value = cast_to_python(state.value)
|
||||
|
||||
return value
|
||||
|
||||
def places_to_area(self, place: Place) -> dict[str, str]:
|
||||
"""Convert places with sub_places to a flat dictionary."""
|
||||
areas = {}
|
||||
|
|
|
@ -6,8 +6,8 @@ from typing import Any
|
|||
from urllib.parse import urlparse
|
||||
|
||||
from pyoverkiz.models import Command, Device
|
||||
from pyoverkiz.types import StateType as OverkizStateType
|
||||
|
||||
from .const import OverkizStateType
|
||||
from .coordinator import OverkizDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/overkiz",
|
||||
"requirements": [
|
||||
"pyoverkiz==1.0.3"
|
||||
"pyoverkiz==1.1.0"
|
||||
],
|
||||
"dhcp": [
|
||||
{
|
||||
|
|
|
@ -6,6 +6,7 @@ from dataclasses import dataclass
|
|||
from typing import cast
|
||||
|
||||
from pyoverkiz.enums import OverkizAttribute, OverkizState, UIWidget
|
||||
from pyoverkiz.types import StateType as OverkizStateType
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -32,7 +33,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import HomeAssistantOverkizData
|
||||
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES, OverkizStateType
|
||||
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
|
||||
from .coordinator import OverkizDataUpdateCoordinator
|
||||
from .entity import OverkizDescriptiveEntity, OverkizEntity
|
||||
|
||||
|
@ -402,6 +403,9 @@ class OverkizStateSensor(OverkizDescriptiveEntity, SensorEntity):
|
|||
if self.entity_description.native_value:
|
||||
return self.entity_description.native_value(state.value)
|
||||
|
||||
if isinstance(state.value, (dict, list)):
|
||||
return None
|
||||
|
||||
return state.value
|
||||
|
||||
|
||||
|
|
|
@ -1740,7 +1740,7 @@ pyotgw==1.1b1
|
|||
pyotp==2.6.0
|
||||
|
||||
# homeassistant.components.overkiz
|
||||
pyoverkiz==1.0.3
|
||||
pyoverkiz==1.1.0
|
||||
|
||||
# homeassistant.components.openweathermap
|
||||
pyowm==3.2.0
|
||||
|
|
|
@ -1097,7 +1097,7 @@ pyotgw==1.1b1
|
|||
pyotp==2.6.0
|
||||
|
||||
# homeassistant.components.overkiz
|
||||
pyoverkiz==1.0.3
|
||||
pyoverkiz==1.1.0
|
||||
|
||||
# homeassistant.components.openweathermap
|
||||
pyowm==3.2.0
|
||||
|
|
Loading…
Add table
Reference in a new issue