Add extra sensors for BMW ConnectedDrive#12591
Add extra sensors for BMW ConnectedDrive#12591MartinHjelmare merged 10 commits intohome-assistant:devfrom gerard33:bmw-connecteddrive
Conversation
|
@ChristianKuehnel I have added the binary sensor, lock and made some small changes to the existing sensor. Can you please review these? |
There was a problem hiding this comment.
Binary sensors don't have a unit of measurement.
There was a problem hiding this comment.
Please sort 🔤 within groups standard library, 3rd party and homeassistant.
There was a problem hiding this comment.
Time stamp is not allowed in state attributes. The state should not update just because of a time change.
There was a problem hiding this comment.
This shows the last update time which is received from the car via the bmw_connected_drive module.
This time stamp is only updated together with other updates received from the car, e.g. an update on the mileage, see the screenshot below.
It's the same usage as in the Mercedes ME component.
There was a problem hiding this comment.
@gerard33
I get your point, I agree it's quite useful to see when the last measurement was taken. But isn't the information redundant? You would also get the age of the information from the headline "3 hours ago".
There was a problem hiding this comment.
Yes, this is redundant, please remove it.
There was a problem hiding this comment.
As a matter of fact it is not redudant. The last update contains the time on which the update from the car is received, but that doesn't always mean that every sensor is being updated. So this can result in a different time shown at the time information in the headline than at the last update time. See the example with the below screenshots.
With this update the door lock state has changed, but the windows state hasn't, resulting in different times for the windows sensor in the headline versus the last update time.
I think it has added value to show this attribute as you can easily see if the sensor has changed or not as part of the last update received from the car.
There was a problem hiding this comment.
If a state attribute changes, the last_updated attribute of the state will be updated with the time, so this information is already available in the the state machine. If a user wants to know about this we offer the template sensor which can display state attributes including last_updated. We shouldn't duplicate this information in the state machine.
There was a problem hiding this comment.
Is lid name lowercase snakecase? That's the home assistant standard.
There was a problem hiding this comment.
It is, this is an example of a lid name: door_driver_front.
There was a problem hiding this comment.
This is iterating windows not lids.
There was a problem hiding this comment.
Changed it to window :)
There was a problem hiding this comment.
Since we register a callback for state updates, I expect that the callback is called when the state has changed on the device, (car lock). Isn't that true? If that is true, the lock method should only call the device lock method.
There was a problem hiding this comment.
Locking/unlocking the car doesn't trigger a state update.
There was a problem hiding this comment.
We're polling the update every 5 minutes. So if we do not change the state here, the UI will show an old state for 5 minutes.
And if we do poll right away, you still get the old state. You have to wait about 10 seconds until you get the correct state from the server.
There was a problem hiding this comment.
I think a middle way is to do an optimistic state update immediately in home assistant, then have the library call the update callback after 10 seconds with the correct state when we are sure that that is present.
There was a problem hiding this comment.
Locking/unlocking the car doesn't trigger a state update.
There was a problem hiding this comment.
See my comment above an this last update time.
MartinHjelmare
left a comment
There was a problem hiding this comment.
It would be best if the interface library made sure the update callback got called upon new state after the lock is locked/unlocked.
There was a problem hiding this comment.
This should probably be handled in the interface library.
There was a problem hiding this comment.
I have removed this check completly in here and will check with @ChristianKuehnel to handle this in the interface library and make that part of a next release.
There was a problem hiding this comment.
Sleeping is not allowed in the executor thread pool.
There was a problem hiding this comment.
Either the update callback should handle setting state or we should do an optimistic state update from here, immediately. The logic below doesn't belong in this method.
There was a problem hiding this comment.
Removed, see above
There was a problem hiding this comment.
Don't use async methods from a sync context.
There was a problem hiding this comment.
Does the name of the sensor also contain the name of the vehicle?
If someone has more than one vehicle, we need to make sure that the sensors then get different names...
There was a problem hiding this comment.
The vehicle name is not show in the sensor name, but it is shown in the attributes. Not sure what is the best approach, but maybe those people with more than one vehicle can update the friendly name of the sensor? Because in my opinion it's not necessary to show the vehicle name in the sensor name if you have one BMW (which will be the majority I guess).
There was a problem hiding this comment.
Should we add an option where people can give names to their vehicles? Something like:
bmw_connected_drive:
my_account:
username: USERNAME_BMW_CONNECTED_DRIVE
password: PASSWORD_BMW_CONNECTED_DRIVE
country: COUNTRY_BMW_CONNECTED_DRIVE
vehicles:
- vin: SOMEVIN
name: his
- vin: SOMEOTHERVIN
name: hersThat way people can
- select which cars should be listed in HASS at all
- set custom names for their vehicles
- we have different names for the different vehicles?
There was a problem hiding this comment.
That is indeed something we could add as an option.
There was a problem hiding this comment.
Please add a comment before this, saying why we need an optimistic state update, when we also have an update callback.
There was a problem hiding this comment.
Added the comments
|
Closing and opening the PR to trigger another Travis CI check. |
|
You changed access permissions for the requirements_all.txt file. Please revert that. That file should only be modified via the Please also revert the access permission change on the component module. |
|
Another close/open for Travis CI. |
There was a problem hiding this comment.
Please update to version 0.4.1. I fixed some exceptions there and it's currently already in use on master
|
@gerard33 Looks good to me. As I updated to bimmer_connected version 0.4.1 on master you probably need to rebase your branch and re-run ./scripcs/gen-requirements. Sorry for that, but I wanted to get the bugfix out quickly... |
MartinHjelmare
left a comment
There was a problem hiding this comment.
Please update the state update logic in all platforms. It will be more efficient and more on line with the rest of home assistant.
Also please remove the time stamp from all state attributes. It's redundant.
With that done this should be good to go.
There was a problem hiding this comment.
Yes, this is redundant, please remove it.
| self._state = bool(vehicle_state.door_lock_state.value | ||
| in ('SELECTIVELOCKED', 'UNLOCKED')) | ||
|
|
||
| self.schedule_update_ha_state() |
There was a problem hiding this comment.
It would be more efficient if you move this to a separate new method which calls this with argument True which will make sure update is called before the state is updated.
Then register this new method as state update callback in async_added_to_hass.
def update_callback(self):
"""Schedule a state update."""
self.schedule_update_ha_state(True)There was a problem hiding this comment.
Updated this, can you please doublecheck if it's okay?
|
|
||
| Show latest data after startup. | ||
| """ | ||
| self._account.add_update_listener(self.update) |
There was a problem hiding this comment.
self._account.add_update_listener(
self.update_callback)| Show latest data after startup. | ||
| """ | ||
| self._account.add_update_listener(self.update) | ||
| yield from self.hass.async_add_job(self.update) |
There was a problem hiding this comment.
Remove this and instead add an argument True to add_devices.
| device = BMWConnectedDriveSensor(account, vehicle, key, | ||
| value[0], value[1]) | ||
| devices.append(device) | ||
| add_devices(devices) |
There was a problem hiding this comment.
add_devices(devices, True)This will make sure update is called before adding the device.
| for vehicle in account.account.vehicles: | ||
| device = BMWLock(account, vehicle, 'lock', 'BMW lock') | ||
| devices.append(device) | ||
| add_devices(devices) |
| self._state = (STATE_LOCKED if vehicle_state.door_lock_state.value | ||
| in ('LOCKED', 'SECURED') else STATE_UNLOCKED) | ||
|
|
||
| self.schedule_update_ha_state() |
|
|
||
| Show latest data after startup. | ||
| """ | ||
| self._account.add_update_listener(self.update) |
| Show latest data after startup. | ||
| """ | ||
| self._account.add_update_listener(self.update) | ||
| yield from self.hass.async_add_job(self.update) |
|
|
||
| def update(self) -> None: | ||
| """Read new state data from the library.""" | ||
| _LOGGER.debug('Updating %s', self.entity_id) |
There was a problem hiding this comment.
Replace this entity_id with name as in the other platforms.
Also please update the state update logic below as in the other platforms.
|
Please add a paragraph and highlight the breaking changes in the description, so it's easy to copy paste that to the release notes. |
|
@MartinHjelmare thanks! |
|
Great! |
|
@MartinHjelmare I just realized that the breaking change for the names of the sensors can be avoided. What do you think? If you agree I can make a PR (including the new binary sensors and lock) asap so this can be part of the HA 0.66 release. |
|
That won't work. We're not allowed to set |
|
Ah, I see. |
|
We should define |
|
Ok. And shall I adapt this PR so there is no breaking change in the name of the current 3 sensors? Then the |
|
If the breaking name change can be avoided, that sounds good. Try to only have one new feature per PR. |



Description:
The following items are added to BMW ConnectedDrive:
Doors,WindowsandDoor lock stateFor the existing sensors
remaining_range_fuel,mileageandremaining_fuelthe attributecar: <car_model>and an applicable mdi icon are added.Breaking changereverted in #13380Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4733
Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests pass