tlyakhov / python-decora_wifi

Python Library for Interacting with Leviton Decora Smart WiFi Switches & Dimmers
MIT License
37 stars 21 forks source link

[WIP] Support Energy API for Smart Breakers #18

Open adamurban opened 3 years ago

adamurban commented 3 years ago

Initial API models for pulling energy data for Smart Breakers.

Example usage:

for residence in all_residences:
    timezone = residence.timezone['id']
    for day_energy in residence.get_energy_consumption_for_day(day = "2021-08-01T00:00:00.000Z"):
         print(day_energy)

    for panel in residence.get_residential_breaker_panels():
        print("Panel: {}".format(panel))
        for breaker in panel.get_residential_breakers():
            print("Energy: " + str(breaker.energyConsumption))
            for energy in breaker.get_energy_consumption_for_year(year = "2021-01-01T00:00:00.000Z", timezone = timezone):
                print(energy)

Example output from breaker.get_energy_consumption_for_month():

{'residentialBreakerPanelId': 'LDATA-ABCDE-12345-FGHIJ', 'residentialBreakerId': '4C4512345678', 'position': 6, 'theDay': '2021-08-11T00:00:00.000Z', 'energyConsumption': 16.2}
{'residentialBreakerPanelId': 'LDATA-ABCDE-12345-FGHIJ', 'residentialBreakerId': '4C4512345678', 'position': 6, 'theDay': '2021-08-12T00:00:00.000Z', 'energyConsumption': 0}
{'residentialBreakerPanelId': 'LDATA-ABCDE-12345-FGHIJ', 'residentialBreakerId': '4C4512345678', 'position': 6, 'theDay': '2021-08-13T00:00:00.000Z', 'energyConsumption': 47.07}
{'residentialBreakerPanelId': 'LDATA-ABCDE-12345-FGHIJ', 'residentialBreakerId': '4C4512345678', 'position': 6, 'theDay': '2021-08-14T00:00:00.000Z', 'energyConsumption': 16.93}
tlyakhov commented 3 years ago

Thank you for the PR! I was kind of hoping Leviton would offer a real API at this point so I could stop maintaining this hacked-together Repo, but alas! :-p

Code looks fine so far. Leave me a comment here when you're ready for a detailed review.

adamurban commented 3 years ago

Yeah, I'm also hoping for a local API, but so far they've made it pretty clear that's not really going to happen :/

Happy for a more detailed review now if you're up for it; I don't currently plan to add support for more API methods at this time than the ones I've included here (unless there is something I missed that's useful for energy metering?), so the remaining work on this side is just cleaning things up.

Some initial questions / thoughts from my side: 1) Do I actually need to store residentialBreakerPanelId as part of the base model, or is there a better way to handle this? 2) What's the best way to handle the required timestamp format/is there a better way to accept a wide variety of input formats and convert to the one the API needs?

Would also love your thoughts on the best way to pull breaker panels into Home Assistant. Could tack on some new data types (beyond switches) (ie, sensors) to the existing decora_wifi integration, or could make a new "leviton_breaker" or similar integration (which would be easier for me I think, but might not be as useful for people that have both in their system).

michaelkkehoe commented 2 years ago

Bump :). I would love to see this merged.

I would suggest that you use a pytz object to store the timezone and then manipulate a datetime to provide a day/ week/ month/ year. Alternatively, the user could provide a string and based off the input type, you format it for the API.

I'm no HA expert, but my thinking is: Ultimately breakers are switches (albeit important ones) . I actually think of it in a similar manner to the TP-Link kasa devices that provide a switch as well as a voltage/ current/ power sensor.