DurgNomis-drol / ha_toyota

Toyota Connected Services integration for Home Assistant.
MIT License
148 stars 29 forks source link

Code rewrite and new statistical sensors #28

Closed DurgNomis-drol closed 3 years ago

DurgNomis-drol commented 3 years ago

BREAKING CHANGES!

This will break existing installations!

Enhancement :

github-actions[bot] commented 3 years ago

🎉 HACS repository validator action summary 🎉 ✅ The repository is not archived ✅ manifest.json file exist ✅ All required keys are present in manifest.json ✅ hacs.json has the 'name' key set ✅ The repository has a description ✅ toyota is added to https://github.com/home-assistant/brands NICE! ✅ The repository has topics ✅ The repository has issues enabled ✅ This day ends with an 'y' (Wednesday) ✅ info.md exists ✅ Requirements validation ✅ Python wheels ❌ HACS load check (more-info: https://hacs.xyz/docs/publish/include#check-hacs )

DurgNomis-drol commented 3 years ago

@CM000n I may have found a way to change weeks stats from Sunday - Saturday to Monday - Sunday. What would you preferer? I believe the Tojota script uses the former.

CM000n commented 3 years ago

Sounds promising. Yes, the Toyota script uses the Japanese week logic from Sunday to Saturday. Personally, of course, I prefer the ISO 8601 standard and thus a week beginning with Monday. Apart from Japan, I think it is only the USA, China, Israel and some Arab countries that have a week beginning with Sunday.

Joonaskaru commented 3 years ago

Hello! I am in favour also to the Mon-Sun cycle. ISO 8601.

DurgNomis-drol commented 3 years ago

@CM000n @Joonaskaru I will implement it in the mytoyota package in the next couple of days, and the change the code here afterwards.

I actually thought more countries used Sunday to Saturday. Nevertheless, all those countries are outside Europe anyways. And i also like to stick to standards, it makes everything easier. 😊

Stay tuned for an update!

DurgNomis-drol commented 3 years ago

Hey guys

I just pushed an update containing the changes we talked about, including some minor improvements on some other areas. 🎉

If you can test it and give me feedback it will be much appreciated 😃

CM000n commented 3 years ago

I just had a strange behavior. After I just replaced the files in my Custom Component Folder with the current versions from the Test Branch, all Toyota sensors were created twice after the restart. I was able to simply remove the old sensors and then delete the "_2" suffix from the duplicate sensors. Any idea what could be the cause of this?

...::Update::... Unfortunately, the new statistic sensors do not seem to work after the update. In the HA logs it says the following:

2021-08-10 22:16:37 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform toyota
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 587, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 711, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 464, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 501, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/toyota/sensor.py", line 162, in extra_state_attributes
    attributes = self.format_statistics_attributes(statistics)
  File "/config/custom_components/toyota/entity.py", line 96, in format_statistics_attributes
    "Average_fuel_consumed": get_average_fuel_consumed(statistics),
  File "/config/custom_components/toyota/entity.py", line 88, in get_average_fuel_consumed
    return fuel_data[FUEL_CONSUMED] + "L/100" + self.mileage_unit
TypeError: unsupported operand type(s) for +: 'float' and 'str'
2021-08-10 22:16:37 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up toyota platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 257, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 587, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 711, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 464, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 501, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/toyota/sensor.py", line 162, in extra_state_attributes
    attributes = self.format_statistics_attributes(statistics)
  File "/config/custom_components/toyota/entity.py", line 96, in format_statistics_attributes
    "Average_fuel_consumed": get_average_fuel_consumed(statistics),
  File "/config/custom_components/toyota/entity.py", line 88, in get_average_fuel_consumed
    return fuel_data[FUEL_CONSUMED] + "L/100" + self.mileage_unit
TypeError: unsupported operand type(s) for +: 'float' and 'str'
2021-08-10 22:16:41 ERROR (MainThread) [homeassistant.components.sensor] toyota: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 431, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 646, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 336, in async_update
    await self.coordinator.async_request_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 141, in async_request_refresh
    await self._debounced_refresh.async_call()
  File "/usr/src/homeassistant/homeassistant/helpers/debounce.py", line 78, in async_call
    await task
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 165, in async_refresh
    await self._async_refresh(log_failures=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 265, in _async_refresh
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 325, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 464, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 501, in _async_write_ha_state
    extra_state_attributes = self.extra_state_attributes
  File "/config/custom_components/toyota/sensor.py", line 162, in extra_state_attributes
    attributes = self.format_statistics_attributes(statistics)
  File "/config/custom_components/toyota/entity.py", line 96, in format_statistics_attributes
    "Average_fuel_consumed": get_average_fuel_consumed(statistics),
  File "/config/custom_components/toyota/entity.py", line 88, in get_average_fuel_consumed
    return fuel_data[FUEL_CONSUMED] + "L/100" + self.mileage_unit
TypeError: unsupported operand type(s) for +: 'float' and 'str'

And this is how it looks in the frontend: 675676576t

It looks like you want to connect numeric and string values via a "+" operator somewhere? Maybe an python fstring is better here, if a link of the values is really necessary? If you like I can look into the code myself during the week. Unfortunately I am currently mostly quite little time :-/

DurgNomis-drol commented 3 years ago

I don't have that attribute with my car, so the error never showed up while i was testing, but it is a very simple fix. It just complains that you cannot add a float variable to a str variable. I have pushed a fix!

Thanks for the feedback!

DurgNomis-drol commented 3 years ago

And the problem with them being add again, is because i changed the unique_id attribute. This was a necessary change to make the code cleaner and better unfortunately. When releasing version 1.0.0 i will make a clean note for every updating from older versions.

CM000n commented 3 years ago

Thank you, the problem seems to be solved now: klngsgt

But aside from the fact that you seem to have forgotten another space 😉, I don't like that the unit for the average fuel consumption is now combined with the value. This makes it much more difficult if you want to extract the value from the entity's attributes for other purposes, e.g. with the help of a template sensor. Also, it breaks a bit with logic, since the other stats don't have a combination of value and unit either. I think it is perfectly sufficient to refer somewhere in the readme to the units of the statistics collected.

DurgNomis-drol commented 3 years ago

I have never been able to use this attribute, so i never thought about it, but you are right, it should only return the value and not anything else.

Update pushed!