StyraHem / ShellyForHASS

Shelly smart home platform for Home Assistant
MIT License
619 stars 111 forks source link

[BUG] assert self.hass is not None after upgrading to HA 0.112.0 #341

Open henricm opened 4 years ago

henricm commented 4 years ago

Environment

Describe the bug

Something seems to have changed in HA 0.112.0 regarding the hass object. I get this error below in the Shelly component.

Steps to Reproduce

Upgrade to HA 0.112.0 and start Home Assistant.

Expected behavior

Screenshots

Traceback/Error logs

2020-07-02 13:17:53 ERROR (Thread-261) [pyShelly] Error update device status: 5A3890 SHRGBW2, , Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pyShelly/block.py", line 132, in update_status_information
    dev.update_status_information(status)
  File "/usr/local/lib/python3.7/site-packages/pyShelly/powermeter.py", line 96, in update_status_information
    self._update(self.state, info_values=self.info_values)
  File "/usr/local/lib/python3.7/site-packages/pyShelly/device.py", line 115, in _update
    self.raise_updated()
  File "/usr/local/lib/python3.7/site-packages/pyShelly/device.py", line 125, in raise_updated
    callback(self)
  File "/config/custom_components/shelly/device.py", line 53, in _updated
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError

Additional context

chemelli74 commented 4 years ago

Please try with 0.1.9b2 and report back.

Simone

henricm commented 4 years ago

I seem to get this error with 0.1.9b2. Is there anything I need to modify in the config? I've tried to remove the integration and add it again. I've also the MQTT integration in HA enabled - could it be they are using the same port as the built in MQTT server in this component? It worked before though...

Traceback (most recent call last):
  File "/config/custom_components/shelly/__init__.py", line 185, in start_up
    pys.start()
  File "/usr/local/lib/python3.7/site-packages/pyShelly/__init__.py", line 129, in start
    self._mqtt.start()
  File "/usr/local/lib/python3.7/site-packages/pyShelly/mqtt.py", line 97, in start
    self._init_socket()
  File "/usr/local/lib/python3.7/site-packages/pyShelly/mqtt.py", line 103, in _init_socket
    sock.bind((self._root.bind_ip, 9955))
OSError: [Errno 98] Address in use
matejzero commented 4 years ago

I'm also experiencing the same problems on latest 0.112.4, event with 0.1.9b3 version. I also run on rPI4 and the problematic Shelly is RGBW2.

I don't have the MQTT enabled and I get no errors from MQTT like @henricm.

oester commented 4 years ago

Similar problem on one of my Shelly's: HA 0.113, ShellyForHass 0.1.9. Not sure how long it's been occuring.

2020-07-26 10:00:31 ERROR (Thread-300) [pyShelly] Error update device status: B942A4-2 SHSW-25, , Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 189, in update_status_information
    dev.update_status_information(status)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/powermeter.py", line 96, in update_status_information
    self._update(self.state, info_values=self.info_values)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 115, in _update
    self.raise_updated()
  File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 125, in raise_updated
    callback(self)
  File "/config/custom_components/shelly/device.py", line 53, in _updated
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError
chemelli74 commented 4 years ago

@henricm and @oester , are you both running HA core ?

Simone

marcelvb commented 4 years ago

I have this problem running the Docker container. And it made my smart home very dumb at the moment ;-)

I got a LOT of these:

2020-07-27 10:06:04 ERROR (CoAP) [pyShelly] Error receive CoAP
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyShelly/coap.py", line 182, in _loop
    self._root.update_block(device_id, device_type,
  File "/usr/local/lib/python3.8/site-packages/pyShelly/__init__.py", line 242, in update_block
    block.update(data, ipaddr)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 92, in update
    dev.update(data)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/dimmer.py", line 40, in update
    self._update(new_state, values, None, self.info_values)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 115, in _update
    self.raise_updated()
  File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 125, in raise_updated
    callback(self)
  File "/config/custom_components/shelly/device.py", line 53, in _updated
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 416, in schedule_update_ha_state
    assert self.hass is not None
AssertionError
chemelli74 commented 4 years ago

Interesting, I'm on core and cannot reproduce your issue.

How are the value of mem and cpu in % ?

Simone

marcelvb commented 4 years ago

CPU% and mem% seem low, as usual. 10-25% max. I have some other errors too, and not sure if they are related.

chemelli74 commented 4 years ago

CPU% and mem% seem low, as usual. 10-25% max. I have some other errors too, and not sure if they are related.

Share them so we can try to better understand your scenario

Simone

matejzero commented 4 years ago

For what is worth, I'm running HassOS with HA 0.112.4. I sometimes have above problem and some times I don't.

Funny thing is some of the shellies are working and some are not, but it's mostly random. Sometimes RGBW is not working, other time one or two Shelly1 are not working, sometimes 2.5... And no way to reproduce, I just reboot HA app until all are working.

chemelli74 commented 4 years ago

A comment on discord about this issue points the finger to a race condition:

The platform implements the Entity interface incorrectly, using it before the entity has been added to home assistant.

Simone

oester commented 4 years ago

@henricm and @oester , are you both running HA core ?

Simone

HA Core; CPU and memory are also low, I don't have a large number of devices.

chemelli74 commented 4 years ago

A comment on discord about this issue points the finger to a race condition:

The platform implements the Entity interface incorrectly, using it before the entity has been added to home assistant.

Simone

Some more details for @hakana:

Make sure to not use the Entity interface before the entity has been added to home assistant, it will work. That's how it should be used. In particular, do not update during setup.

Maybe an update message came in during setup and this created the race condition.

Suggestion from dev is to "Subscribe in async_added_to_hass() https://developers.home-assistant.io/docs/core/entity/#async_added_to_hass"

Simone

matejzero commented 4 years ago

Is this expected to be fixed with the new release with CoAP protocol update and shelly 1.8 firmware?

chemelli74 commented 4 years ago

Is this expected to be fixed with the new release with CoAP protocol update and shelly 1.8 firmware?

Nothing related to protocol or firmware. Plugin needs to be updated to follow HA best practice to create entity interfaces.

Simone

mpolivanov commented 3 years ago

I have to deal with same issue, especially after I restart my wifi routers - all devices turn unavailable and does not come back for a while (10 minutens?). Full error stack/log entry:

`Log Details (ERROR) Logger: pyShelly Source: helpers/entity.py:407 First occurred: 11 September 2020, 21:25:34 (800 occurrences) Last logged: 12:55:36

Error update device status: F39AC0 SHDM-1, , Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 178, in update_status_information dev.update_status_information(status) File "/usr/local/lib/python3.8/site-packages/pyShelly/dimmer.py", line 72, in update_status_information self._update(SRC_STATUS, new_state, values) File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 109, in _update self.raise_updated() File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError Error update device status: F39AC0 SHDM-1, , Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 179, in update_status_information dev.raise_updated() File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError Error update loop, , Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/init.py", line 288, in _update_loop block.check_available() File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 121, in check_available dev.raise_updated(True) File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError Error update device status: C4CD09 SHSW-25, , Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 178, in update_status_information dev.update_status_information(status) File "/usr/local/lib/python3.8/site-packages/pyShelly/roller.py", line 82, in update_status_information self._update(SRC_STATUS, state) File "/usr/local/lib/python3.8/site-packages/pyShelly/device.py", line 109, in _update self.raise_updated() File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError Error update device status: C4CD09-1 SHSW-25, , Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 179, in update_status_information dev.raise_updated() File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pyShelly/init.py", line 288, in _update_loop block.check_available() File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 121, in check_available dev.raise_updated(True) File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated callback(self) File "/config/custom_components/shelly/device.py", line 55, in _updated self.schedule_update_ha_state(True) File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 407, in schedule_update_ha_state assert self.hass is not None AssertionError`

matejzero commented 3 years ago

Are you running the latest release (0.2.0)? If not, could you try if that fixes the problem, since there has been a massive rewrite in 0.2.0 and that part might also be fixed?

I switched to MQTT, so I can't test and my friend is running on Proxmox with NVMe drives and the systems boots up fast enough for this to not be an issue, so we can't test.

mpolivanov commented 3 years ago

I do: Home Assistant 0.114.4 ShellyForHass (Shelly integration) 0.2.0

matejzero commented 3 years ago

Ugh, I hoped this would be fixed with 0.2.0. Sorry, can't help here then.

henricm commented 3 years ago

I updated to 0.2.0 and it seems to work fine now! Great work everyone! So the original problem this issue was created for seems to be solved now.

matejzero commented 3 years ago

@mpolivanov reports the problem even with 0.2.0.

mpolivanov commented 3 years ago

@mpolivanov reports the problem even with 0.2.0.

Meanwhile I've removed all devices and plugin from hass, reinstalled them again and the error is still there

First occurred: 14 September 2020, 13:37:23 (101490 occurrences)

dashdrum commented 3 years ago

I'm a newcomer to docker, but I'm trying the lastest home-assistant docker image. After seeing some similar errors to these I did some poking around and found that the version of pyShelly is quite old in the docker image. It is beyond my skills to know how to update the image. Maybe this will help someone working on this.

etricky commented 3 years ago

I'm also seeing this error every time the devices are pooled. Running latest version of ShellyForHASS 0.2.1 and HA 2020.12.2. Furthermore, when I restart HA, all entities become unavailable and I have to add the integration again in order to get the entities working again!

2021-01-03 19:53:08 DEBUG (Poll status) [pyShelly] Update id:B945F0-1 state:False stateValue:{'last_event': '', 'event_cnt': 0}
2021-01-03 19:53:08 ERROR (Poll status) [pyShelly] Error update device status: B945F0-1 SHSW-25, , Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 191, in update_status_information
    dev.raise_updated(force_update_devices)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated
    callback(self)
  File "/config/custom_components/shelly/device.py", line 59, in _updated
    self._update_ha_state()
  File "/config/custom_components/shelly/device.py", line 51, in _update_ha_state
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 422, in schedule_update_ha_state
    assert self.hass is not None
AssertionError
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/pyShelly/block.py", line 191, in update_status_information
    dev.raise_updated(force_update_devices)
  File "/usr/local/lib/python3.8/site-packages/pyShelly/base.py", line 36, in raise_updated
    callback(self)
  File "/config/custom_components/shelly/device.py", line 59, in _updated
    self._update_ha_state()
  File "/config/custom_components/shelly/device.py", line 51, in _update_ha_state
    self.schedule_update_ha_state(True)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 422, in schedule_update_ha_state
    assert self.hass is not None
AssertionError
iz3man commented 3 years ago

https://github.com/StyraHem/ShellyForHASS/issues/516#issuecomment-767533372