Skip to content

Commit c5e17e3

Browse files
committed
Major Stability and Error fix
1 parent 4232284 commit c5e17e3

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

custom_components/wyzesense/binary_sensor.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
DOMAIN = "wyzesense"
3030

31+
STORAGE = ".storage/wyzesense.json"
32+
3133
ATTR_MAC = "mac"
3234
ATTR_RSSI = "rssi"
3335
ATTR_AVAILABLE = "available"
@@ -49,6 +51,16 @@
4951

5052
_LOGGER = logging.getLogger(__name__)
5153

54+
def getStorage(hass):
55+
if not path.exists(hass.config.path(STORAGE)):
56+
return []
57+
with open(hass.config.path(STORAGE),'r') as f:
58+
return json.load(f)
59+
60+
def setStorage(hass,data):
61+
with open(hass.config.path(STORAGE),'w') as f:
62+
json.dump(data, f)
63+
5264
def findDongle():
5365
df = subprocess.check_output(["ls", "-la", "/sys/class/hidraw"]).decode('utf-8').lower()
5466
for l in df.split('\n'):
@@ -85,8 +97,12 @@ def on_event(ws, event):
8597
new_entity = WyzeSensor(data)
8698
entities[event.MAC] = new_entity
8799
add_entites([new_entity])
88-
f = open('.storage/wyze_sensors.txt', 'a')
89-
f.write(event.MAC + "\r\n")
100+
101+
storage = getStorage(hass)
102+
if event.MAC not in storage:
103+
storage.append(event.MAC)
104+
setStorage(hass, storage)
105+
90106
else:
91107
entities[event.MAC]._data = data
92108
entities[event.MAC].schedule_update_ha_state()
@@ -97,16 +113,11 @@ def beginConn():
97113

98114
ws = beginConn()
99115

100-
result = []
116+
storage = getStorage(hass)
101117

102-
if path.exists('.storage/wyze_sensors.txt'):
103-
sensor_file = open('.storage/wyze_sensors.txt')
104-
result = sensor_file.readlines()
105-
sensor_file.close()
118+
_LOGGER.debug("%d Sensors Loaded from storage" % len(storage))
106119

107-
_LOGGER.debug("%d Sensors Loaded from config" % len(result))
108-
109-
for mac in result:
120+
for mac in storage:
110121
_LOGGER.debug("Registering Sensor Entity: %s" % mac)
111122

112123
mac = mac.strip()
@@ -154,6 +165,11 @@ def on_remove(call):
154165
toDelete = entities[mac]
155166
hass.add_job(toDelete.async_remove)
156167
del entities[mac]
168+
169+
storage = getStorage(hass)
170+
storage.remove(mac)
171+
setStorage(hass, storage)
172+
157173
notification = "Successfully Removed Sensor: %s" % mac
158174
hass.components.persistent_notification.create(notification, DOMAIN)
159175
_LOGGER.debug(notification)
@@ -226,4 +242,4 @@ def device_state_attributes(self):
226242
del attributes[ATTR_STATE]
227243
del attributes[ATTR_AVAILABLE]
228244

229-
return attributes
245+
return attributes

info.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Changelog
2+
### 0.0.7 - Major Stability and Error fix
3+
**WHEN UPGRADING TO THIS VERSION YOU WILL HAVE TO RETRIGGER EACH SENSOR TO HAVE IT DISPLAY AGAIN**
4+
5+
This is a one time thing you have to do once updating and restarting HomeAssistant. Once you do this, stability on restarts should improve dramatically. This specifically solves any errors that contain `result = ws.List()` in the error message. This is the vast majority of errors reported.

0 commit comments

Comments
 (0)