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

Include charging state for powerwall #33432

Merged
merged 2 commits into from
Apr 20, 2020

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Mar 30, 2020

Proposed change

This allows homekit to show the charging state in the
homekit app when the powerwall is exported as a linked
battery sensor.

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

Example entry for configuration.yaml:

# Example configuration.yaml

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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

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

@project-bot project-bot bot added this to Needs review in Dev Mar 30, 2020
@project-bot project-bot bot moved this from Needs review to By Code Owner in Dev Mar 30, 2020
@frenck
Copy link
Member

frenck commented Mar 30, 2020

Just a small question that serves to trigger the brain:

Why make this an attribute and not a binary sensor? It makes it easier to automate and display.

@bdraco
Copy link
Member Author

bdraco commented Mar 30, 2020

Just a small question that serves to trigger the brain:

Why make this an attribute and not a binary sensor? It makes it easier to automate and display.

homekit wants it as an attribute:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/homekit/accessories.py#L127

@bdraco
Copy link
Member Author

bdraco commented Mar 30, 2020

@balloob
Copy link
Member

balloob commented Mar 31, 2020

And Homekit is having that code because that's how we decided it once upon a time in the iOS app… 😆

I think that it's better served as a binary sensor.

@bdraco
Copy link
Member Author

bdraco commented Mar 31, 2020

And Homekit is having that code because that's how we decided it once upon a time in the iOS app… 😆

I think that it's better served as a binary sensor.

There isn't a way to have the linked battery track the sensor charge level and the binary sensor charging state currently so I think I'm kind stuck with this unless I'm missing something

@balloob
Copy link
Member

balloob commented Mar 31, 2020

In the frontend we do:

state -> entity registry -> device registry -> all other entities for this device -> filter for binary_sensor with device class == X.

@bdraco
Copy link
Member Author

bdraco commented Mar 31, 2020

In the frontend we do:

state -> entity registry -> device registry -> all other entities for this device -> filter for binary_sensor with device class == X.

Homekit doesn't implement linking a binary sensor and sensor for the linked_battery_sensor

homekit:
  entity_config:
    binary_sensor.powerwall_status:
      linked_battery_sensor: sensor.powerwall_charge
      low_battery_threshold: 10

Screenshot 2020-03-31 at 3 16 18 PM

@bdraco bdraco force-pushed the powerwall_show_charging_homekit branch from e8cdfa5 to bd198db Compare March 31, 2020 20:18
@balloob
Copy link
Member

balloob commented Mar 31, 2020

Users shouldn't set up linking, this is something that homekit should discover itself by seeing what the device supports. We should add a helper functions to device registry entry to help with this DeviceEntry.get_type(domain, device_class)

@bdraco
Copy link
Member Author

bdraco commented Mar 31, 2020

Users shouldn't set up linking, this is something that homekit should discover itself by seeing what the device supports. We should add a helper functions to device registry entry to help with this DeviceEntry.get_type(domain, device_class)

I agree, its should show up if there a proper device entry.

I'm thinking implementation looks something like:

  1. Add a new device class to binary sensors for charging.. Maybe DEVICE_CLASS_BATTERY_CHARGING since we already have DEVICE_CLASS_BATTERY for On means low, Off means normal
  2. Update homekit to look in the device registry and link DEVICE_CLASS_BATTERY_CHARGING to the charging Char and DEVICE_CLASS_BATTERY to the % charged.
  3. Deprecate linked_battery_sensor from homekit and remove it a few versions from now.

@balloob
Copy link
Member

balloob commented Mar 31, 2020

Yeah that sounds good.

@bdraco
Copy link
Member Author

bdraco commented Apr 1, 2020

Yeah that sounds good.

@balloob
What is the best way to lookup the device_info for a given entity id?

https://github.com/home-assistant/core/blob/dev/homeassistant/components/homekit/accessories.py#L105

@balloob
Copy link
Member

balloob commented Apr 1, 2020

ent_reg = await entity_registry.async_get_registry(hass)
dev_reg = await device_registry.async_get_registry(hass)

entry = ent_reg.async_get(entity_id)

if entry is None or entry.device_id is None:
    return

entries = entity_registry.async_entries_for_device(ent_reg, entry.device_id)

for entry in entries:
    if entry.domain == "binary_sensor" and entry.device_class == X:
        return entry

return None

@bdraco
Copy link
Member Author

bdraco commented Apr 2, 2020

ent_reg = await entity_registry.async_get_registry(hass)

@balloob Thanks for that.

I put together a proof of concept for feedback:
#33519

@MartinHjelmare
Copy link
Member

I'll move this to incoming in our project task board. Ping here when the architecture issue is merged and we can continue review here.

@MartinHjelmare MartinHjelmare moved this from By Code Owner to Incoming in Dev Apr 9, 2020
@MartinHjelmare MartinHjelmare moved this from Incoming to By Code Owner in Dev Apr 15, 2020
@bdraco bdraco force-pushed the powerwall_show_charging_homekit branch from bd198db to 2476f8e Compare April 18, 2020 20:05
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.

Looks good!

Dev automation moved this from By Code Owner to Reviewer approved Apr 19, 2020
@bdraco
Copy link
Member Author

bdraco commented Apr 19, 2020

Thanks @MartinHjelmare

Will merge after I have a day of sun to revalidate this.

@bdraco
Copy link
Member Author

bdraco commented Apr 20, 2020

After the sun went down, the sensor stayed off. The margin of error seems to be set right to avoid false on readings.

@bdraco bdraco merged commit 75e5f08 into home-assistant:dev Apr 20, 2020
Dev automation moved this from Reviewer approved to Done Apr 20, 2020
@lock lock bot locked and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants