@@ -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
10156class 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
0 commit comments