Skip to content

Commit

Permalink
add eco sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
siku2 committed Aug 13, 2023
1 parent 98a6b77 commit b528b67
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 60 deletions.
12 changes: 7 additions & 5 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
"ms-python.vscode-pylance"
],
"ms-python.python",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"ms-python.isort"
],
"settings": {
"files.eol": "\n",
"editor.tabSize": 4,
Expand Down
55 changes: 32 additions & 23 deletions custom_components/vzug/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
"""The V-ZUG integration."""

import dataclasses
import enum
import logging
import typing
from collections.abc import Awaitable, Coroutine
from datetime import timedelta
from typing import Any
from collections.abc import Awaitable

import yarl

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

Expand Down Expand Up @@ -59,48 +56,44 @@ class Data:
notifications: list[api.PushNotification]


_T = typing.TypeVar("_T")

class DeviceCategory(enum.StrEnum):
ADORA_DISH = enum.auto()
ADORA_WASH = enum.auto()

async def _wait_or_none(awaitable: Awaitable[_T], *, msg: str) -> _T | None:
try:
return await awaitable
except Exception:
_LOGGER.exception(msg)
@classmethod
def from_model_description(cls, desc: str):
desc = desc.lower()
if "adorawash" in desc:
return cls.ADORA_WASH
if "adoradish" in desc:
return cls.ADORA_DISH
return None


class Coordinator(DataUpdateCoordinator[Data]):
api: "api.VZugApi"
device_info: DeviceInfo
unique_id_prefix: str
_mac_addr: str | None
_model_description: str | None
category: DeviceCategory | None

def __init__(
self,
hass: HomeAssistant,
base_url: yarl.URL,
*,
update_interval: timedelta | None = None,
request_refresh_debouncer: Debouncer[Coroutine[Any, Any, None]] | None = None,
always_update: bool = True,
) -> None:
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=update_interval,
request_refresh_debouncer=request_refresh_debouncer,
always_update=always_update,
)

self.api = api.VZugApi(async_get_clientsession(hass), base_url)
self.device_info = DeviceInfo(manufacturer="V-ZUG")
self.unique_id_prefix = ""
self.category = None

self._mac_addr = None
self._model_description = None
self._mac_addr: str | None = None
self._model_description: str | None = None

async def _async_update_data(self) -> Data:
try:
Expand Down Expand Up @@ -130,6 +123,11 @@ async def _async_update_data(self) -> Data:
self.api.get_model_description(), msg="model description"
)

if self.category is None and self._model_description is not None:
self.category = DeviceCategory.from_model_description(
self._model_description
)

if self.unique_id_prefix == "":
self.unique_id_prefix = device["deviceUuid"]

Expand Down Expand Up @@ -161,3 +159,14 @@ async def _async_update_data(self) -> Data:
hh_fw_version=hh_fw_version,
notifications=notifications,
)


_T = typing.TypeVar("_T")


async def _wait_or_none(awaitable: Awaitable[_T], *, msg: str) -> _T | None:
try:
return await awaitable
except Exception:
_LOGGER.exception(msg)
return None
6 changes: 5 additions & 1 deletion custom_components/vzug/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import time
import typing

Expand Down Expand Up @@ -122,7 +123,8 @@ async def _command(
params: dict[str, str] | None = None,
raw: bool = False,
expected_type: typing.Any = None,
attempts: int = 3,
attempts: int = 5,
retry_delay: float = 1.0
) -> typing.Any:
if params is None:
params = {}
Expand All @@ -149,6 +151,8 @@ async def once() -> typing.Any:
return await once()
except aiohttp.ClientResponseError as exc:
last_exc = exc
await asyncio.sleep(retry_delay)

raise last_exc

async def get_mac_address(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion custom_components/vzug/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import voluptuous as vol
import yarl

from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
Expand Down
Loading

0 comments on commit b528b67

Please sign in to comment.