Skip to content

Commit 27cf96f

Browse files
authored
Merge branch 'main' into filter_sensors
2 parents 1664246 + e57e234 commit 27cf96f

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@
33
With a lot of inspiration and help from https://github.com/Apollon77/node-ph803w project I ported the basic functionallity to python with the hope and expectation to deploy it as an Home Assistant intagration without need for a docker service and MQTT.
44
Still a lot left to do but at least it can poll the device and show values in Home Assistant.
55
In desktop mode (running cyclic with lib/main.py) it performs a bit better.
6+
7+
## Installation of HA component
8+
9+
1. Clone this repo as `ph803w` dir into `$HA_CONFIG_DIR/custom_components/`
10+
```
11+
$ cd custom_components
12+
$ git clone git@github.com:dala318/ph803w.git ./ph803w
13+
```
14+
2. Setup `$HA_CONFIG_DIR/configuration.yaml`
15+
16+
```yaml
17+
ph803w:
18+
host: 192.168.1.2 # IP of your device
19+
```

__init__.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
_LOGGER = logging.getLogger(__name__)
2424

2525
UPDATE_TOPIC = f"{DOMAIN}_update"
26-
SCAN_INTERVAL = timedelta(seconds=10)
27-
ERROR_INTERVAL = timedelta(seconds=300)
28-
MAX_FAILS = 10
26+
ERROR_ITERVAL_MAPPING = [10, 60, 300, 600, 3000, 6000]
2927
NOTIFICATION_ID = "ph803w_device_notification"
3028
NOTIFICATION_TITLE = "PH-803W Device status"
3129

@@ -80,6 +78,7 @@ def __init__(self, hass, device_client: device.Device) -> None:
8078
self.hass = hass
8179
self.device_client = device_client
8280
self.device_client.register_callback(self.dispatcher_new_data)
81+
self.device_client.register_callback(self.reset_fail_counter)
8382
self.host = self.device_client.host
8483
self._shutdown = False
8584
self._fails = 0
@@ -113,30 +112,26 @@ def shutdown(event):
113112
_LOGGER.debug("Graceful shutdown")
114113
return
115114

116-
if self._fails > MAX_FAILS:
117-
_LOGGER.error("Failed to reconnect. Thread stopped")
118-
persistent_notification.create(
119-
self.hass,
120-
"Error:<br/>Connection to PH-803W device failed "
121-
"the maximum number of times. Thread has stopped",
122-
title=NOTIFICATION_TITLE,
123-
notification_id=NOTIFICATION_ID,
124-
)
125-
return
126-
127115
try:
128116
self.device_client.run(once=False)
129-
except (device.DeviceError, RecursionError):
117+
except (device.DeviceError, RecursionError, ConnectionError):
130118
_LOGGER.exception("Failed to read data, attempting to recover")
131119
self.device_client.close()
132120
self._fails += 1
133-
sleep_time = self._fails * ERROR_INTERVAL.total_seconds()
121+
error_mapping = self._fails
122+
if error_mapping >= len(ERROR_ITERVAL_MAPPING):
123+
error_mapping = len(ERROR_ITERVAL_MAPPING) - 1
124+
sleep_time = ERROR_ITERVAL_MAPPING[error_mapping]
134125
_LOGGER.debug(
135126
"Sleeping for fail #%s, in %s seconds", self._fails, sleep_time
136127
)
137128
self.device_client.reset_socket()
138129
time.sleep(sleep_time)
139130

131+
@callback
132+
def reset_fail_counter(self):
133+
self._fails = 0
134+
140135
@callback
141136
def dispatcher_new_data(self):
142137
"""Noyifying HASS that new data is ready to read."""

lib/device.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ async def run_async(self, once: bool = True) -> bool:
4343

4444
def run(self, once: bool = True) -> bool:
4545
self._loop = True
46+
if self._socket.fileno() == -1:
47+
self.reset_socket()
48+
self._connect()
4649
if once:
47-
if self._socket.fileno() == -1:
48-
self.reset_socket()
49-
self._connect()
5050
return self._run(once)
5151
else:
52-
if self._socket.fileno() == -1:
53-
self.reset_socket()
54-
self._connect()
5552
self._run(once)
5653
return not self._loop
5754

0 commit comments

Comments
 (0)