diff --git a/custom_components/wyzesense/binary_sensor.py b/custom_components/wyzesense/binary_sensor.py index 9bd8233..e0a64cc 100644 --- a/custom_components/wyzesense/binary_sensor.py +++ b/custom_components/wyzesense/binary_sensor.py @@ -28,6 +28,8 @@ DOMAIN = "wyzesense" +STORAGE = ".storage/wyzesense.json" + ATTR_MAC = "mac" ATTR_RSSI = "rssi" ATTR_AVAILABLE = "available" @@ -49,6 +51,16 @@ _LOGGER = logging.getLogger(__name__) +def getStorage(hass): + if not path.exists(hass.config.path(STORAGE)): + return [] + with open(hass.config.path(STORAGE),'r') as f: + return json.load(f) + +def setStorage(hass,data): + with open(hass.config.path(STORAGE),'w') as f: + json.dump(data, f) + def findDongle(): df = subprocess.check_output(["ls", "-la", "/sys/class/hidraw"]).decode('utf-8').lower() for l in df.split('\n'): @@ -85,8 +97,12 @@ def on_event(ws, event): new_entity = WyzeSensor(data) entities[event.MAC] = new_entity add_entites([new_entity]) - f = open('.storage/wyze_sensors.txt', 'a') - f.write(event.MAC + "\r\n") + + storage = getStorage(hass) + if event.MAC not in storage: + storage.append(event.MAC) + setStorage(hass, storage) + else: entities[event.MAC]._data = data entities[event.MAC].schedule_update_ha_state() @@ -97,16 +113,11 @@ def beginConn(): ws = beginConn() - result = [] + storage = getStorage(hass) - if path.exists('.storage/wyze_sensors.txt'): - sensor_file = open('.storage/wyze_sensors.txt') - result = sensor_file.readlines() - sensor_file.close() + _LOGGER.debug("%d Sensors Loaded from storage" % len(storage)) - _LOGGER.debug("%d Sensors Loaded from config" % len(result)) - - for mac in result: + for mac in storage: _LOGGER.debug("Registering Sensor Entity: %s" % mac) mac = mac.strip() @@ -154,6 +165,11 @@ def on_remove(call): toDelete = entities[mac] hass.add_job(toDelete.async_remove) del entities[mac] + + storage = getStorage(hass) + storage.remove(mac) + setStorage(hass, storage) + notification = "Successfully Removed Sensor: %s" % mac hass.components.persistent_notification.create(notification, DOMAIN) _LOGGER.debug(notification) @@ -226,4 +242,4 @@ def device_state_attributes(self): del attributes[ATTR_STATE] del attributes[ATTR_AVAILABLE] - return attributes + return attributes \ No newline at end of file diff --git a/info.md b/info.md new file mode 100644 index 0000000..11a3e27 --- /dev/null +++ b/info.md @@ -0,0 +1,5 @@ +## Changelog +### 0.0.7 - Major Stability and Error fix +**WHEN UPGRADING TO THIS VERSION YOU WILL HAVE TO RETRIGGER EACH SENSOR TO HAVE IT DISPLAY AGAIN** + +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. \ No newline at end of file