core: The Tradfri integration stops working every few days.

The problem

The Tradfri integration stops working every few days. A restart of the integration (or hass entirely) is required to revive it. No need to restart the ikea tradfri hub.

Environment

  • Home Assistant Core release with the issue: 0.116.4
  • Last working Home Assistant Core release (if known): 0.114.x
  • Operating environment (OS/Container/Supervised/Core): Container on x86
  • Integration causing this issue: tradfri
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/tradfri/

Problem-relevant configuration.yaml

Configured via flow

Traceback/Error logs

2020-10-28 17:05:08 ERROR (MainThread) [homeassistant.core] Error executing service: <ServiceCall light.turn_on (c:081e8aff193811ebac0de104ad2b18e1): entity_id=['light.tradfri_bulb_e27_ws_opal_980lm'], params=brightness=175, color_temp=370>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1351, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1370, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 470, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 655, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 507, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 249, in async_handle_light_on_service
    await light.async_turn_on(**params)
  File "/usr/src/homeassistant/homeassistant/components/tradfri/light.py", line 262, in async_turn_on
    await self._api(command)
  File "/usr/src/homeassistant/homeassistant/components/tradfri/base_class.py", line 22, in wrapper
    await func(command)
  File "/usr/local/lib/python3.8/site-packages/pytradfri/api/aiocoap_api.py", line 164, in request
    result = await self._execute(api_commands)
  File "/usr/local/lib/python3.8/site-packages/pytradfri/api/aiocoap_api.py", line 148, in _execute
    _, res = await self._get_response(msg)
  File "/usr/local/lib/python3.8/site-packages/pytradfri/api/aiocoap_api.py", line 96, in _get_response
    r = await pr.response
  File "/usr/local/lib/python3.8/site-packages/aiocoap/protocol.py", line 731, in _run_outer
    await cls._run(app_request, response, weak_observation, protocol, log)
  File "/usr/local/lib/python3.8/site-packages/aiocoap/protocol.py", line 783, in _run
    blockresponse = await blockrequest.response
  File "/usr/local/lib/python3.8/site-packages/aiocoap/protocol.py", line 313, in send
    request_interface.request(plumbing_request)
  File "/usr/local/lib/python3.8/site-packages/aiocoap/tokenmanager.py", line 243, in request
    self.outgoing_requests[key] = request
TypeError: 'NoneType' object does not support item assignment
2020-10-28 17:05:08 ERROR (MainThread) [homeassistant.components.tradfri.base_class] Unable to execute command <Command put ['15001', 65544]: {'3311': [{'5711': 370}]}>: ('There was an error with the request.', CredentialsMissingError('No suitable credentials for coaps://192.168.1.3:5684/15001/65544'))
2020-10-28 17:05:08 ERROR (MainThread) [homeassistant.components.tradfri.base_class] Unable to execute command <Command put ['15001', 65544]: {'3311': [{'5851': 175}]}>: ('There was an error with the request.', CredentialsMissingError('No suitable credentials for coaps://192.168.1.3:5684/15001/65544'))
2020-10-28 17:05:08 ERROR (MainThread) [homeassistant.components.tradfri.base_class] Unable to execute command <Command put ['15001', 65538]: {'3311': [{'5850': 1}]}>: ('There was an error with the request.', CredentialsMissingError('No suitable credentials for coaps://192.168.1.3:5684/15001/65538'))
2020-10-28 17:13:12 ERROR (MainThread) [coap] Connection loss was not expected.

Additional information

Originally discussed here: https://community.home-assistant.io/t/tradfri-integration-stops-working-approx-once-twice-a-week/238429/4 I’ll see if the issue keeps appearing on the 0.117.x

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 168 (56 by maintainers)

Most upvoted comments

It seems to getting worse by the day here, just minutes after a HA reboot. I have had to restart the IKEA integration almost every time I want to switch the light (since I’m not doing it every minute). I also notice that, when it happens, the IKEA remotes stops working as well. These should be unrelated to HA, so it seems something’s happening to the Trådfri gateway or mesh when it freezes. Reading forums, I’ve seen this problem going years back. As the dev. said in the latest HA release party: The least we can expect from an automated home is to be able to switch our light on and off.

I am also experiencing this consistently, reloading the integration resolves it until the next time I interact with it. Has there been any progress on this!

Great news! I certainly wasn’t alone doing this. Without the patient support of @MartinHjelmare this wouldn’t have happened.

Neither did I, the integration has been rock-solid for quite some time now. Thanks for the effort @ggravlingen ! Let’s close it.

Cool reports guys. Well screaming Me Too is utterly useless in FOSS concept. Even so more, when you are even not trying to describe your problems in any structured way, which could help others to troubleshoot this one. But as I have unlimited patience, I will try to help you. Disclaimer: I have no connection to HA or NabuCasa, just passing by.

Tradfri integration uses two libraries (one part of HA, second is 3rd party) which both lack proper handling of all possible error use cases. It is as it is and if you are not willing to help by yourself, you will have to patiently wait until someone will fix it for all of us.

Meanwhile, you can decrease possible chances for errors, especially ones cause by your network. So please spent your time on connecting your Tradfri GW to HA via Ethernet cable. I have my GW connected in this way and from bug fix which was delivered at the end of 2020 my integration is rock stable - except rare re-connection issues after GW is restarted.

Release 2020.12.2 contains the fix for this issue.

@rubenbe yes please, that would be great. It’s good for the HA project that there are more of us working on patches and fixes. 😃

I can take this whole merging administration up if you don’t have the time @ggravlingen

Is still an issue? There have been updates to the gateway and we’ve made quite a few improvements to the Tradfri code in Home Assistant as well since the beginning of the year.

No issues, HA has been rocksolid with trådfri for months now for me.

Even the issues I had with the trådfri itself, lights not turning on or off after firmware updates seem to have gone away.

Is still an issue?

Sample size of one, but I haven’t had to restart my tradfri integration for a couple of months now. I’d say it’s fixed.

I haven’t had any issue for a couple of months at least!

there is serious development in the underlying library https://github.com/home-assistant-libs/pytradfri/pull/392 being merged into dev currently. Lets keep our fingers crossed this will aid to the fix.

thanks to Martin and al who have been busy with the issue!

Can’t understand why we are still dealing this issue 1 year later. Is there another way of asking support to the team ???

These last couples of days i had to restat Ikea HUB AND HA once a day, this is very annyoing 😕

I have the same issue but running HA on a synology NAS 24/7…

And its dead. Rolled back to 2021.9.1. Update: Now dead with 2021.9.1, too.

Hei @jonaslorander. Head over to https://developers.home-assistant.io/ for dev info. On https://developers.home-assistant.io/docs/development_environment you get a guide on a setup with VS Code and dev on a container.

I’d just like to report the following:

  • before the patch in 2020.12.2 I had switched over to ethernet, which has drastically reduced the instability. If I remember correctly, I didn’t have to reload the integration at all actually
  • after upgrading the 2020.12.2 patch I’ve had to reload the integration once, however in relation to the tradfri hub being unavailable (unsure whether there was a firmware update or if the hub was unplugged)

In any case, great job for anyone who has helped. And to reiterate what has been said before: wired connection works a lot better for this integration

This integration is really hit and miss. Sometimes it will work for months, and then, like now; it works for a few hours only. I always remove and set-up integration when I have problems, but this is ridiculous right now. None of the automations have worked for the last few days.

I’m also seeing this. I get a few hours, before I have to readd again.

HA 2021.1.5 Python Version | 3.8.7

This is where client_credentials are populated inside of pytradfri:

    async def generate_psk(self, security_key):
        """Generate and set a psk from the security key."""
        if not self._psk:
            # Set context once for generating key
            protocol = await self._get_protocol()
            command = Gateway().generate_psk(self._psk_id)
            protocol.client_credentials.load_from_dict(
                {
                    f"coaps://{self._host}:5684/{command.path_str}": {
                        "dtls": {
                            "psk": security_key.encode("utf-8"),
                            "client-identity": "Client_identity".encode("utf-8"),
                        }
                    }
                }
            )

            self._psk = await self.request(command)

            # aiocoap has now cached our psk, so it must be reset.
            # We also no longer need the protocol, so this will clean that up.
            await self._reset_protocol()
            await self._update_credentials()

    async def _update_credentials(self):
        """Update credentials."""
        protocol = await self._get_protocol()
        protocol.client_credentials.load_from_dict(
            {
                f"coaps://{self._host}:5684/*": {
                    "dtls": {
                        "psk": self._psk.encode("utf-8"),
                        "client-identity": self._psk_id.encode("utf-8"),
                    }
                }
            }
        )

Based on that code I think I see the issue. While protocol is re-created after being destroyed upon “Protocol reset”, no credentials are re-loaded.

So IMHO something like this COULD help:

    async def _get_protocol(self):
        """Get the protocol for the request."""
        if self._protocol is None:
            self._protocol = asyncio.create_task(Context.create_client_context())
            self._protocol.client_credentials.load_from_dict(
                {
                    f"coaps://{self._host}:5684/*": {
                        "dtls": {
                            "psk": self._psk.encode("utf-8"),
                            "client-identity": self._psk_id.encode("utf-8"),
                        }
                    }
                }
        return await self._protocol

But is is terrible hack due to possible client_credentials duplicated call - it shall be rewritten better if it helps 😃

Same issues here. practically every morning I wake up to the Integration being broken. Any way to automate the reload of the integration?

Have a look in the new version of HA if it works better, a fix was recently added to the code base.