|
| 1 | +""" """ |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import time |
| 5 | +from xmlrpc.client import boolean |
| 6 | + |
| 7 | +from dukaonesdk.device import Device |
| 8 | +from dukaonesdk.dukaclient import DukaClient |
| 9 | + |
| 10 | +from homeassistant.helpers.typing import HomeAssistantType |
| 11 | + |
| 12 | +from . import DukaEntityComponent |
| 13 | +from .const import DOMAIN |
| 14 | + |
| 15 | +_LOGGER = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class DukaEntity: |
| 19 | + """Implement the base of a duka entity with a reference to a device""" |
| 20 | + |
| 21 | + def __init__(self, hass: HomeAssistantType, device_id): |
| 22 | + self._device_id = device_id |
| 23 | + self.device: Device = None |
| 24 | + component: DukaEntityComponent = hass.data[DOMAIN] |
| 25 | + self.the_client: DukaClient = component.the_client |
| 26 | + |
| 27 | + def initialize_device(self, password: str, ip_address: str) -> None: |
| 28 | + """Initialize the duka one device""" |
| 29 | + self.device = self.the_client.add_device( |
| 30 | + self._device_id, |
| 31 | + password, |
| 32 | + ip_address, |
| 33 | + self.on_change, |
| 34 | + ) |
| 35 | + |
| 36 | + async def wait_for_device_to_be_ready(self) -> boolean: |
| 37 | + """Wait for the device to reponse to the initial get firmware version command.""" |
| 38 | + timeout = time.time() + 10 |
| 39 | + while self.device is None or self.device.firmware_version is None: |
| 40 | + if time.time() > timeout: |
| 41 | + return False |
| 42 | + await asyncio.sleep(0.1) |
| 43 | + return True |
| 44 | + |
| 45 | + def on_change(self, device: Device): |
| 46 | + """Callback whe dukaone has changes - must be implemented in derived class""" |
| 47 | + raise NotImplementedError() |
| 48 | + |
| 49 | + def dukaone_device_info(self): |
| 50 | + """Return device information.""" |
| 51 | + if self.device is None: |
| 52 | + return None |
| 53 | + info = { |
| 54 | + "name": "DukaOne", |
| 55 | + "identifiers": {(DOMAIN, self._device_id)}, |
| 56 | + "manufacturer": "Duka Ventilation", |
| 57 | + "model": f"Type {self.device.unit_type}", |
| 58 | + "sw_version": f"{self.device.firmware_version} {self.device.firmware_date}", |
| 59 | + } |
| 60 | + return info |
0 commit comments