|
| 1 | +"""A demonstration 'hub' that connects several devices.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +# In a real implementation, this would be in an external library that's on PyPI. |
| 5 | +# The PyPI package needs to be included in the `requirements` section of manifest.json |
| 6 | +# See https://developers.home-assistant.io/docs/creating_integration_manifest |
| 7 | +# for more information. |
| 8 | +# This dummy hub always returns 3 rollers. |
| 9 | +import asyncio |
| 10 | +import socket |
| 11 | +import random |
| 12 | + |
| 13 | +from .lib import discovery, device |
| 14 | + |
| 15 | +from homeassistant.core import HomeAssistant |
| 16 | + |
| 17 | +PH803W_DEFAULT_TCP_PORT = 12416 |
| 18 | + |
| 19 | + |
| 20 | +class Hub: |
| 21 | + """Dummy hub for Hello World example.""" |
| 22 | + |
| 23 | + manufacturer = "Demonstration Corp" |
| 24 | + |
| 25 | + def __init__(self, hass: HomeAssistant, host: str) -> None: |
| 26 | + """Init dummy hub.""" |
| 27 | + # discover = discovery.Discovery() |
| 28 | + # res = discover.run() |
| 29 | + dev = device.Device(host) |
| 30 | + res = dev.run() |
| 31 | + # dev.close() |
| 32 | + |
| 33 | + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 34 | + self.socket.connect((host, PH803W_DEFAULT_TCP_PORT)) |
| 35 | + |
| 36 | + data = bytes.fromhex("0000000303000006") |
| 37 | + self.socket.sendall(data) |
| 38 | + response = self.socket.recv(1024) |
| 39 | + passcode_lenth = response[9] |
| 40 | + passcode_raw = response[10 : 10 + passcode_lenth] |
| 41 | + passcode = passcode_raw.decode("utf-8") |
| 42 | + |
| 43 | + data = ( |
| 44 | + bytes.fromhex("000000030f00000800") |
| 45 | + + passcode_lenth.to_bytes(1, "little") |
| 46 | + + passcode_raw |
| 47 | + ) |
| 48 | + self.socket.sendall(data) |
| 49 | + response = self.socket.recv(1024) |
| 50 | + if response[8] != 0: |
| 51 | + print("Error connecting") |
| 52 | + |
| 53 | + # Connection established, from now on some cyclig bahavior |
| 54 | + data = bytes.fromhex("000000030400009002") |
| 55 | + self.socket.sendall(data) |
| 56 | + empty_counter = 0 |
| 57 | + data = bytes.fromhex("0000000303000015") |
| 58 | + response = self.socket.recv(1024) |
| 59 | + # if len(response) == 0: |
| 60 | + # empty_counter += 1 |
| 61 | + # continue |
| 62 | + empty_counter = 0 |
| 63 | + # print(response) |
| 64 | + if len(response) == 18: |
| 65 | + flag1 = response[8] |
| 66 | + if flag1 & 0b0000_0100: |
| 67 | + print("In water") |
| 68 | + flag2 = response[9] |
| 69 | + if flag2 & 0b0000_0010: |
| 70 | + print("ORP on") |
| 71 | + if flag2 & 0b0000_0001: |
| 72 | + print("PH on") |
| 73 | + # state_raw = response[8 : 9] |
| 74 | + ph_raw = response[10:12] |
| 75 | + self.ph = int.from_bytes(ph_raw, "big") * 0.01 |
| 76 | + redox_raw = response[12:14] |
| 77 | + self.redox = int.from_bytes(redox_raw, "big") - 2000 |
| 78 | + unknown1_raw = response[14:16] |
| 79 | + unknown1 = int.from_bytes(unknown1_raw, "big") |
| 80 | + unknown2_raw = response[15:18] |
| 81 | + unknown2 = int.from_bytes(unknown2_raw, "big") |
| 82 | + |
| 83 | + self._host = host |
| 84 | + self._hass = hass |
| 85 | + self._name = host |
| 86 | + self._id = host.lower() |
| 87 | + self.probe = Probe(f"{self._id}_1", f"{self._name} 1", self) |
| 88 | + self.online = True |
| 89 | + |
| 90 | + @property |
| 91 | + def hub_id(self) -> str: |
| 92 | + """ID for dummy hub.""" |
| 93 | + return self._id |
| 94 | + |
| 95 | + async def test_connection(self) -> bool: |
| 96 | + """Test connectivity to the Dummy hub is OK.""" |
| 97 | + await asyncio.sleep(1) |
| 98 | + return True |
| 99 | + |
| 100 | + |
| 101 | +class Probe: |
| 102 | + """Dummy roller (device for HA) for Hello World example.""" |
| 103 | + |
| 104 | + def __init__(self, id: str, name: str, hub: Hub) -> None: |
| 105 | + """Init dummy roller.""" |
| 106 | + self._id = id |
| 107 | + self.hub = hub |
| 108 | + self.name = name |
| 109 | + self._ph_value = hub.ph |
| 110 | + self._orp_value = hub.redox |
| 111 | + self._callbacks = set() |
| 112 | + self._loop = asyncio.get_event_loop() |
| 113 | + |
| 114 | + # Some static information about this device |
| 115 | + self.firmware_version = f"0.0.{random.randint(1, 9)}" |
| 116 | + self.model = "Test Device" |
| 117 | + |
| 118 | + @property |
| 119 | + def id(self) -> str: |
| 120 | + """Return ID for roller.""" |
| 121 | + return self._id |
| 122 | + |
| 123 | + # async def set_position(self, position: int) -> None: |
| 124 | + # """ |
| 125 | + # Set dummy cover to the given position. |
| 126 | + |
| 127 | + # State is announced a random number of seconds later. |
| 128 | + # """ |
| 129 | + # self._target_position = position |
| 130 | + |
| 131 | + # # Update the moving status, and broadcast the update |
| 132 | + # self.moving = position - 50 |
| 133 | + # await self.publish_updates() |
| 134 | + |
| 135 | + # self._loop.create_task(self.delayed_update()) |
| 136 | + |
| 137 | + async def delayed_update(self) -> None: |
| 138 | + """Publish updates, with a random delay to emulate interaction with device.""" |
| 139 | + await asyncio.sleep(random.randint(1, 10)) |
| 140 | + self.moving = 0 |
| 141 | + await self.publish_updates() |
| 142 | + |
| 143 | + def register_callback(self, callback: Callable[[], None]) -> None: |
| 144 | + """Register callback, called when Roller changes state.""" |
| 145 | + self._callbacks.add(callback) |
| 146 | + |
| 147 | + def remove_callback(self, callback: Callable[[], None]) -> None: |
| 148 | + """Remove previously registered callback.""" |
| 149 | + self._callbacks.discard(callback) |
| 150 | + |
| 151 | + # In a real implementation, this library would call it's call backs when it was |
| 152 | + # notified of any state changeds for the relevant device. |
| 153 | + async def publish_updates(self) -> None: |
| 154 | + """Schedule call all registered callbacks.""" |
| 155 | + pass |
| 156 | + # self._current_position = self._target_position |
| 157 | + # for callback in self._callbacks: |
| 158 | + # callback() |
| 159 | + |
| 160 | + @property |
| 161 | + def online(self) -> float: |
| 162 | + """Roller is online.""" |
| 163 | + # The dummy roller is offline about 10% of the time. Returns True if online, |
| 164 | + # False if offline. |
| 165 | + return random.random() > 0.1 |
| 166 | + |
| 167 | + @property |
| 168 | + def ph(self) -> float: |
| 169 | + """Battery level as a percentage.""" |
| 170 | + return self._ph_value |
| 171 | + |
| 172 | + @property |
| 173 | + def orp(self) -> float: |
| 174 | + """Return a random voltage roughly that of a 12v battery.""" |
| 175 | + return self._orp_value |
0 commit comments