Skip to content

Commit 296b015

Browse files
committed
Working a bit more
1 parent 617554f commit 296b015

3 files changed

Lines changed: 30 additions & 84 deletions

File tree

hub.py

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,23 @@ class Hub:
2424

2525
def __init__(self, hass: HomeAssistant, host: str) -> None:
2626
"""Init dummy hub."""
27+
# Discovery is not working in Home Assistant, could it be something to do with UDP broadcast?
2728
# discover = discovery.Discovery()
2829
# 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")
30+
31+
with device.Device(host) as d:
32+
self.online = d.run(once=True)
33+
result = d.get_latest_measurement_and_empty()
34+
uid = d.passcode
35+
36+
if not self.online:
37+
return
8238

8339
self._host = host
8440
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
41+
self._name = "pH-803w"
42+
self._id = uid.lower()
43+
self.probe = Probe(f"{self._id}", f"{self._name}", self, result)
8944

9045
@property
9146
def hub_id(self) -> str:
@@ -94,20 +49,25 @@ def hub_id(self) -> str:
9449

9550
async def test_connection(self) -> bool:
9651
"""Test connectivity to the Dummy hub is OK."""
97-
await asyncio.sleep(1)
98-
return True
52+
with device.Device(self._host) as d:
53+
return d.run(once=True)
9954

10055

10156
class Probe:
10257
"""Dummy roller (device for HA) for Hello World example."""
10358

104-
def __init__(self, id: str, name: str, hub: Hub) -> None:
59+
def __init__(
60+
self, id: str, name: str, hub: Hub, result: device.Measurement
61+
) -> None:
10562
"""Init dummy roller."""
10663
self._id = id
10764
self.hub = hub
10865
self.name = name
109-
self._ph_value = hub.ph
110-
self._orp_value = hub.redox
66+
self._ph_value = result.ph
67+
self._orp_value = result.redox
68+
self._ph_on = result.ph_on
69+
self._opp_on = result.orp_on
70+
self._in_water = result.in_water
11171
self._callbacks = set()
11272
self._loop = asyncio.get_event_loop()
11373

@@ -120,20 +80,6 @@ def id(self) -> str:
12080
"""Return ID for roller."""
12181
return self._id
12282

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-
13783
async def delayed_update(self) -> None:
13884
"""Publish updates, with a random delay to emulate interaction with device."""
13985
await asyncio.sleep(random.randint(1, 10))
@@ -166,10 +112,10 @@ def online(self) -> float:
166112

167113
@property
168114
def ph(self) -> float:
169-
"""Battery level as a percentage."""
115+
"""Return the pH measurement."""
170116
return self._ph_value
171117

172118
@property
173119
def orp(self) -> float:
174-
"""Return a random voltage roughly that of a 12v battery."""
120+
"""Return the ORP measurement."""
175121
return self._orp_value

lib/device.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _run(self, once: bool = True) -> bool:
105105

106106
if once and len(self._measurements) > 0:
107107
self._loop = False
108-
108+
self.close()
109109
return (once and len(self._measurements) > 0) or not once
110110

111111
def _handle_response(self, data):
@@ -195,7 +195,10 @@ def abort(self):
195195
self._loop = False
196196

197197
def close(self):
198-
self._socket.close()
198+
try:
199+
self._socket.close()
200+
except:
201+
pass
199202

200203
def get_latest_measurement_and_empty(self):
201204
if len(self._measurements) > 0:
@@ -205,11 +208,10 @@ def get_latest_measurement_and_empty(self):
205208
return None
206209

207210
def __enter__(self):
208-
self.run()
209211
return self
210212

211213
def __exit__(self, type, value, traceback):
212-
self._socket.close()
214+
self.close()
213215

214216

215217
class Measurement:

sensor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ def __init__(self, probe):
8686
# The name of the entity
8787
self._attr_name = f"{self._probe.name} pH"
8888

89-
self._state = 7.2
90-
9189
@property
9290
def state(self):
9391
"""Return the state of the sensor."""

0 commit comments

Comments
 (0)