Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes battery_life attribute because sometimes the JSON return a str instead of an int #92

Merged
merged 1 commit into from
May 28, 2018

Conversation

tchellomello
Copy link
Owner

@tchellomello tchellomello commented May 26, 2018

Fixes battery_life attribute because sometimes the JSON return a str instead of an int

Fixes: #91

>>>from ring_doorbell import Ring
>>>from pprint import pprint
>>>myring = Ring('user', 'pass', 'true')
>>>obj = myring.doorbells[0] #Front Door door bell
>>>pprint(vars(obj))
{'_attrs': {u'address': u'address',
            u'alerts': {u'connection': u'online'},
            u'battery_life': u'58',
            u'description': u'Front Door',
....
'_health_attrs': {u'average_signal_category': u'good',
                   u'average_signal_strength': -49,
                   u'battery_percentage': u'58',
                   u'battery_percentage_category': u'good',
                   u'battery_voltage': None,
                   u'battery_voltage_category': None,
                   u'firmware': u'Up to Date',

>>>print(obj.battery_life)
100
>>>print (obj._attrs["battery_life"])
58
>>>print(obj._health_attrs.get('battery_percentage'))
58

The issue happens when the attribute is a string

In [1]: a = u'58'

In [2]: a > 100
Out[2]: True

To fix, basically let's cast that value:

In [1]: a = u'58'

In [2]: a > 100
Out[2]: True

In [3]: int(a) > 100
Out[3]: False

@tchellomello
Copy link
Owner Author

@mrwhale PR created. Could you please check if that fixes the issue?

@tchellomello tchellomello changed the title Fixes battery_life attribute because sometimes the JSON return an str instead of an int Fixes battery_life attribute because sometimes the JSON return a str instead of an int May 26, 2018
@coveralls
Copy link

coveralls commented May 26, 2018

Coverage Status

Coverage remained the same at 65.854% when pulling 1536856 on fix_91 into 599b4a5 on master.

@mrwhale
Copy link

mrwhale commented May 28, 2018

👍 boom. Thanks @tchellomello that's done the trick. Works for both classes too

>>>from ring_doorbell import Ring
>>>from ring_doorbell import RingDoorBell
>>>from pprint import pprint

>>>myring = Ring('creds', 'true')
>>>obj = myring.doorbells[1] #Front door
>>>obj2 = RingDoorBell(myring, 'Front Door')

>>>print(obj.battery_life)
57
>>>print(obj2.battery_life)
57
>>>print (obj._attrs["battery_life"])
57
>>>print (obj2._attrs["battery_life"])
57

@tchellomello
Copy link
Owner Author

Awesome let's commit this and then I'll submit a PR for home assistant too.

@tchellomello tchellomello merged commit 6b6d97b into master May 28, 2018
@tchellomello tchellomello deleted the fix_91 branch May 28, 2018 04:28
@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] Battery life shows 100 when its not
3 participants