bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
937 stars 250 forks source link

Cannot uncheck "Create energy sensor" - argument of type 'NoneType' is not iterable #1214

Closed michaelblight closed 1 year ago

michaelblight commented 1 year ago

I had set up sensor using YAML but found I was getting "_2" entities created despite using "unique_id". So I deleted the YAML code and restarted HA. Then I recreated the powercalc sensors using the UI.

When I created the virtual entities for the group, I realised I accidentally left "create energy sensor" checked. Later when I went back to re-configure each and uncheck it, some failed the the error "argument of type 'NoneType' is not iterable" (see below for details).

There doesn't seem to be a pattern: some of my Hue lights could be unchecked, and others could not; neither of my LIFX could be unchecked; the one light that was a group (an HA group, not a powercalc group) could not. Not a big deal, but not sure how to rectify.

I'm on v1.0.1 of powercalc.

Logger: aiohttp.server
Source: custom_components/powercalc/power_profile/library.py:80
Integration: Powercalc ([documentation](https://github.com/bramstroker/homeassistant-powercalc), [issues](https://github.com/bramstroker/homeassistant-powercalc/issues))
First occurred: 6:23:06 PM (2 occurrences)
Last logged: 6:28:21 PM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 222, in forwarded_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 82, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 136, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 239, in post
    return await super().post(request, flow_id)
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 73, in wrapper
    result = await method(view, request, data, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 110, in post
    result = await self._flow_mgr.async_configure(flow_id, data)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 280, in async_configure
    result = await self._async_handle_step(
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 367, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/config/custom_components/powercalc/config_flow.py", line 472, in async_step_init
    errors = await self.save_options(user_input)
  File "/config/custom_components/powercalc/config_flow.py", line 509, in save_options
    strategy_object = await _create_strategy_object(
  File "/config/custom_components/powercalc/config_flow.py", line 555, in _create_strategy_object
    power_profile = await ProfileLibrary.factory(hass).get_profile(
  File "/config/custom_components/powercalc/power_profile/library.py", line 80, in get_profile
    if "/" in model_info.model:
TypeError: argument of type 'NoneType' is not iterable
bramstroker commented 1 year ago

Which version of powercalc are you running? When not on the latest version I recommend to upgrade first to v1.0.1. If it is still not working for you could you please sent met the contents of config/.storage/core.config_entries? You can sent them to bgerritsen at gmail dot com. And please highlight which config entries you have trouble with opening the options dialog.

bramstroker commented 1 year ago

with v1.0.2 I have implemented the option to download diagnostics. When you click the three dots on the config entry you have troubles with you should have the option to download. You can include the report here as it shouldn't contain any sensitive information.

michaelblight commented 1 year ago

Here's the diagnostics... I'm going to upgrade to HA 2022.11 in the next day or so, but don't imagine that will make much difference. I've also included a screenshot of the sensors in case that helps. The line numbers in the error log changed a bit too, so I have included that as well.

image

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.9.1",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.5",
    "docker": true,
    "arch": "x86_64",
    "timezone": "Australia/Sydney",
    "os_name": "Linux",
    "os_version": "5.15.60",
    "supervisor": "2022.10.2",
    "host_os": "Home Assistant OS 8.5",
    "docker_version": "20.10.14",
    "chassis": "vm",
    "run_as_root": true
  },
  "custom_components": {
    "powerpal": {
      "version": "0.1.0",
      "requirements": [
        "mindmelting.powerpal==0.3.0"
      ]
    },
    "spotcast": {
      "version": "v3.6.30",
      "requirements": [
        "spotify_token==1.0.0"
      ]
    },
    "xiaomi_miio_airpurifier": {
      "version": "2022.8.0.0",
      "requirements": [
        "construct==2.10.56",
        "python-miio>=0.5.12"
      ]
    },
    "public_transport_victoria": {
      "version": "0.3.2",
      "requirements": []
    },
    "bureau_of_meteorology": {
      "version": "1.1.14",
      "requirements": [
        "iso8601"
      ]
    },
    "deepstack_object": {
      "version": "4.6.0",
      "requirements": [
        "pillow",
        "deepstack-python==0.8"
      ]
    },
    "mass": {
      "version": "2022.8.4",
      "requirements": [
        "music-assistant==1.8.7"
      ]
    },
    "grocy": {
      "version": "v4.5.2",
      "requirements": [
        "pygrocy==1.4.1"
      ]
    },
    "google_keep": {
      "version": "v1.1.4",
      "requirements": [
        "gkeepapi==0.11.16"
      ]
    },
    "places": {
      "version": "1.9",
      "requirements": []
    },
    "login_control": {
      "version": "1.0.0",
      "requirements": []
    },
    "eufy_security": {
      "version": "2.7.0",
      "requirements": [
        "websocket-client==1.1.0"
      ]
    },
    "bhyve": {
      "version": "3.0.0",
      "requirements": []
    },
    "hacs": {
      "version": "1.27.1",
      "requirements": [
        "aiogithubapi>=22.2.4"
      ]
    },
    "watchman": {
      "version": "0.5.1",
      "requirements": [
        "prettytable==3.0.0"
      ]
    },
    "nest_protect": {
      "version": "0.3.8",
      "requirements": []
    },
    "nodered": {
      "version": "1.1.2",
      "requirements": []
    },
    "powercalc": {
      "version": "v1.0.2",
      "requirements": [
        "numpy>=1.21.1"
      ]
    },
    "smartir": {
      "version": "1.17.6",
      "requirements": [
        "aiofiles==0.6.0"
      ]
    }
  },
  "integration_manifest": {
    "after_dependencies": [
      "integration",
      "utility_meter"
    ],
    "codeowners": [
      "@bramstroker"
    ],
    "config_flow": true,
    "dependencies": [
      "light",
      "group",
      "template",
      "select",
      "utility_meter"
    ],
    "documentation": "https://github.com/bramstroker/homeassistant-powercalc",
    "domain": "powercalc",
    "iot_class": "local_polling",
    "issue_tracker": "https://github.com/bramstroker/homeassistant-powercalc/issues",
    "name": "Powercalc",
    "requirements": [
      "numpy>=1.21.1"
    ],
    "version": "v1.0.2",
    "is_built_in": false
  },
  "data": {
    "entry": {
      "entry_id": "7366f3537761d280512181ffdce75d93",
      "version": 1,
      "domain": "powercalc",
      "title": "Entrance",
      "data": {
        "entity_id": "light.entrance",
        "mode": "lut",
        "create_energy_sensor": true,
        "create_utility_meters": false,
        "name": "Entrance",
        "unique_id": "powercalc.entrance",
        "sensor_type": "virtual_power"
      },
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": "powercalc.entrance",
      "disabled_by": null
    }
  }
}

Here's the data section for another one with the error. Quite similar, so I assume all those erroring look the same.

  "data": {
    "entry": {
      "entry_id": "dea330df4ccaf5193b72db5013a8b155",
      "version": 1,
      "domain": "powercalc",
      "title": "Hue W1",
      "data": {
        "entity_id": "light.hue_w1",
        "mode": "lut",
        "create_energy_sensor": true,
        "create_utility_meters": false,
        "name": "Hue W1",
        "unique_id": "powerpal.hue_w1",
        "group": "5122f1b845357f03677020f34e21046e",
        "sensor_type": "virtual_power"
      },
      "options": {},
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": "powerpal.hue_w1",
      "disabled_by": null
    }
  }
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 222, in forwarded_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 82, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 136, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 239, in post
    return await super().post(request, flow_id)
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 73, in wrapper
    result = await method(view, request, data, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 110, in post
    result = await self._flow_mgr.async_configure(flow_id, data)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 280, in async_configure
    result = await self._async_handle_step(
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 367, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/config/custom_components/powercalc/config_flow.py", line 548, in async_step_init
    errors = await self.save_options(user_input)
  File "/config/custom_components/powercalc/config_flow.py", line 586, in save_options
    strategy_object = await _create_strategy_object(
  File "/config/custom_components/powercalc/config_flow.py", line 638, in _create_strategy_object
    power_profile = await ProfileLibrary.factory(hass).get_profile(
  File "/config/custom_components/powercalc/power_profile/library.py", line 76, in get_profile
    if "/" in model_info.model:
TypeError: argument of type 'NoneType' is not iterable
bramstroker commented 1 year ago

The problem is manufacturer and model are missing in the config entry data. When calculation strategy is "lut" this information must also be available.

How did you setup these entities? Were they discovered by powercalc and you just accepted that, or did you create the config entry yourself by clicking "Add Integration" -> "Powercalc"?

michaelblight commented 1 year ago

In all cases I used "Add Integration" -> "Powercalc". I didn't realise it did discovery. When I originally had it as "lut" in the YAML, I did so in the UI as well. In all these cases, after entering "lut", the addon worked out what the device was and I just said "ok". I can't remember if any behaved slightly differently.

So I deleted the entry and recreated it with "Create energy sensor" selected again. Then I changed it to unselected and it worked! However, I did get a new screen during the creation that didn't occur before, as shown below. Is this new with 1.0.2? I was on 1.0.1. I may go back and set them all to "ignore unavailable" as this looks useful. So the upshot is I can no longer reproduce the problem, so I guess you should close this until somebody has the same issue. Thanks for your help! image

bramstroker commented 1 year ago

Ah, glad you got it resolved. Think you might have created the config entry when there were still some bugs.

This discovery is new feature since v1.0.0. It won't show when you already setup a sensor for your light, but when new models are added and something is found on your system matching the manufacturer/model it will show a discovered integration which you can directly add.

This additional screen is relatively new and allows you to set any additional advanced options (didn't wont to clutter the main configuration options with all these extra option). You can just click submit to skip these.

bramstroker commented 1 year ago

Btw are you also able to change the options now for the config entry you just setup again?

michaelblight commented 1 year ago

Yes. Although, while I was deleting and recreating for testing, my "All lights" powercalc group ended up with this... image I don't know what that "7366..." is, and have no entity called that - is it an internal device id? In these cases I was adding it to the group while creating the new virtual power entity. I tried deleting/adding some more and sometimes got different strange ids. But also the delete was not always actually deleting - rather than taking me back to the list of powercalc entities like this... image It would leave me with "Hue W1" selected. This was hard to reproduce, but is perhaps the culprit for "half deleted" entities. Especially late on a Saturday night after a few wines.

bramstroker commented 1 year ago

I see, the id you see is the ID of the config entry you deleted. I need to write some code to also update all associated groups so the entity is also removed from the group. For now you can manually click the cross to delete it from the group.

Not sure about your other issue. Also keep an eye on the error log for any errors.

bramstroker commented 1 year ago

Fix for removal on associated groups is already implemented. Closing this issue. Let me know if you have any other enquiries.