natekspencer / pylitterbot

Python package for controlling a Whisker connected self-cleaning litter boxes and feeders
MIT License
87 stars 11 forks source link

Fix for deleted robots #160

Closed ianatha closed 1 year ago

ianatha commented 1 year ago

Fixes #159

File "~/.asdf/installs/python/3.11.4/lib/python3.11/site-packages/pylitterbot/robot/litterrobot3.py", line 133, in _parse_sleep_info
    map(int, sleep_mode_active[1:].split(":"))
             ~~~~~~~~~~~~~~~~~^^^^
TypeError: 'NoneType' object is not subscriptable
ianatha commented 1 year ago

From what I understand, the underlying issue is that null JSON values are parsed as None. This is indeed a pervasive issue across many fields.

It seems that a get(key, default_value) method for dict that returns the default_value not only when the key isn't in the dict, but also when the corresponding value in the dict is None would solve this issue across the board.

Also added a simple test case based on the data my deleted robot is returning.

@natekspencer let me know what you think

natekspencer commented 1 year ago

From what I understand, the underlying issue is that null JSON values are parsed as None. This is indeed a pervasive issue across many fields.

Correct

It seems that a get(key, default_value) method for dict that returns the default_value not only when the key isn't in the dict, but also when the corresponding value in the dict is None would solve this issue across the board.

While it may solve for issues where we want a non-None value, it may introduce problems where we actually want to check for the None value and act on that. Without a more thorough examination, to arbitrarily adjust the dict at this point is not recommended.

I think in this scenario, where the only information seems to be an id and a name, it would actually be better to not try to load a robot at all. i.e. we should check if litterRobotSerial is null and not create a robot for that particular array element.

natekspencer commented 1 year ago

That looks good to me. I'll wait for @tkdrob's input before merging right away.