jscruz / sensor.carbon_intensity_uk

Carbon Intensity UK Sensor for Home Assistant
MIT License
8 stars 11 forks source link

Error in icon intensity = INTENSITY[index] KeyError: 'very high' #7

Open dr-harper opened 2 years ago

dr-harper commented 2 years ago

I was running into an error with this plugin when the carbon intensity of the grid was "very high".

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 522, in _async_add_entity
    original_icon=entity.icon,
  File "/config/custom_components/carbon_intensity_uk/sensor.py", line 38, in icon
    intensity = INTENSITY[index]
KeyError: 'very high'

I managed to get this working again by changing the const.py file to include versions of the values without the underscore:

INTENSITY = {
    "very_low" : 0,
    "very low" : 0,
    "low" : 1,
    "moderate": 2,
    "high": 3,
    "very_high": 4,
    "very high": 4,
}

I haven't properly tested this but it seems to be working. It might be possible to delete the very_low and very_high values.

jscruz commented 2 years ago

Thanks @dr-harper, I'll fix this issue in the next release.