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

Convert solarlog to coordinator #55345

Merged
merged 4 commits into from
Aug 27, 2021
Merged

Convert solarlog to coordinator #55345

merged 4 commits into from
Aug 27, 2021

Conversation

balloob
Copy link
Member

@balloob balloob commented Aug 27, 2021

Breaking change

Proposed change

This converts the solarlog integration to use a data update coordinator. I did not test this PR as I don't have a solarlog device.

@Ernst79 can you test this?

Fixes #55254

There is still room for improvement to this integration I think, like storing the SolarLog object directly instead of converting it to a dict and moving the calculations into the SolarLogSensorEntityDescription object. But that I leave for someone else :)

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

Hey there @Ernst79, mind taking a look at this pull request as it has been labeled with an integration (solarlog) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@balloob balloob mentioned this pull request Aug 27, 2021
22 tasks
@balloob balloob added this to the 2021.9.0 milestone Aug 27, 2021
@Ernst79
Copy link
Contributor

Ernst79 commented Aug 27, 2021

Yes, thanks. I noticed that it sends a api.time = datetime.datetime(1999, 1, 1, 1, 1, 1) in the wrong data packets, I’ll think it is better to use that for filtering out these messages.

if data["TIME"] == datetime(1999, 1, 1, 1, 1, 1):

we have to change the first line from datetime import datetime, timedelta

I do notice that the error isn’t added to the log. If I add a manual _LOGGER.error, it is added, so the if condition is correct.

@balloob
Copy link
Member Author

balloob commented Aug 27, 2021

Alright pushed a fix. It just looks if the year is 1999 and halts on that.


data = {}

try:
Copy link
Contributor

@Ernst79 Ernst79 Aug 27, 2021

Choose a reason for hiding this comment

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

the pypi package seems to either send all data, or nothing {}, so I think we can do

        if api:
            data["TIME"] = api.time
            data["powerAC"] = api.power_ac
            data["powerDC"] = api.power_dc
            data["voltageAC"] = api.voltage_ac
            data["voltageDC"] = api.voltage_dc
            data["yieldDAY"] = api.yield_day / 1000
            data["yieldYESTERDAY"] = api.yield_yesterday / 1000
            data["yieldMONTH"] = api.yield_month / 1000
            data["yieldYEAR"] = api.yield_year / 1000
            data["yieldTOTAL"] = api.yield_total / 1000
            data["consumptionAC"] = api.consumption_ac
            data["consumptionDAY"] = api.consumption_day / 1000
            data["consumptionYESTERDAY"] = api.consumption_yesterday / 1000
            data["consumptionMONTH"] = api.consumption_month / 1000
            data["consumptionYEAR"] = api.consumption_year / 1000
            data["consumptionTOTAL"] = api.consumption_total / 1000
            data["totalPOWER"] = api.total_power
            data["alternatorLOSS"] = api.alternator_loss
            data["CAPACITY"] = round(api.capacity * 100, 0)
            data["EFFICIENCY"] = round(api.efficiency * 100, 0)
            data["powerAVAILABLE"] = api.power_available
            data["USAGE"] = round(api.usage * 100, 0)

            if api.time.year == 1999:
                raise update_coordinator.UpdateFailed(
                    "Invalid data returned (can happen after Solarlog restart)"
                )
        else:
            raise update_coordinator.UpdateFailed(
                "Solarlog response returns no data"
            )

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, just posted the above comment while you added your last commit. Modified it.

Thanks for your help.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that will work because api is a SolarLog object

Copy link
Member Author

Choose a reason for hiding this comment

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

If the current code works I suggest we go with that and you can clean it up in a future PR. Since I don't have a device to test it with, I don't want to do more than necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I'll do a final test.

@Ernst79
Copy link
Contributor

Ernst79 commented Aug 27, 2021

The only thing that is strange is that I NEVER see an error in the HA logs about "Invalid data returned (can happen after Solarlog restart)". When I add a _LOGGER.error("Here has an error occurred") on the line before that, I do see an error in the HA logs, so the condition is met.

It also does seem to halt on the raised exception, as I don't get the zero values in the graphs and the sensors become unavailable for a few minutes. But, I was expecting that it logs the error in the HA log as well.

Not very important to fix now. I can look into that later myself.

@balloob balloob merged commit 46d0523 into dev Aug 27, 2021
@balloob balloob deleted the solarlog-coordinator branch August 27, 2021 21:59
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Just a future improvement.

@property
def native_value(self) -> StateType:
"""Return the native sensor value."""
return self.coordinator.data[self.entity_description.json_key]
Copy link
Member

@MartinHjelmare MartinHjelmare Aug 28, 2021

Choose a reason for hiding this comment

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

Side note for the future: If we rename the keys in the data returned by the coordinator to the same names as the entity description keys, we can drop the json_key from the entity description and use the entity description key instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, But that will cause a change in the unique_id. I don’t think that is desired.

self._attr_unique_id = f"{coordinator.unique_id}_{description.key}"

Copy link
Member

Choose a reason for hiding this comment

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

No, we won't change the description key attribute just remove the json_key attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

But I need the JSON key to get the correct value from the sunwatcher pypi package. This package is having partly capitalized works as keys in its output

Copy link
Member

Choose a reason for hiding this comment

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

We don't use the json_key to get the data from the pypi library. We use it to get the data from the coordinator, which is data we define in this integration. We can define that data dict however we want.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry, you are totally right. I’ll add that in a future PR

balloob added a commit that referenced this pull request Aug 28, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Aug 29, 2021
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.

4 participants