Skip to content

Commit

Permalink
Major Stability and Error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinvincent committed Jan 14, 2020
1 parent 4232284 commit c5e17e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
38 changes: 27 additions & 11 deletions custom_components/wyzesense/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

DOMAIN = "wyzesense"

STORAGE = ".storage/wyzesense.json"

ATTR_MAC = "mac"
ATTR_RSSI = "rssi"
ATTR_AVAILABLE = "available"
Expand All @@ -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'):
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -226,4 +242,4 @@ def device_state_attributes(self):
del attributes[ATTR_STATE]
del attributes[ATTR_AVAILABLE]

return attributes
return attributes
5 changes: 5 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -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.

3 comments on commit c5e17e3

@kevinvincent
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@photinus Thanks for implementing saving the mac ids to a local file. I made some changes to your implementation as a result of #74

@photinus
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kevinvincent, Sorry I haven't been more active the last few weeks.

@jstinesnc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this AWESOME integration for Home Assistant, @kevinvincent ! This is working perfectly for me.

Please sign in to comment.