Skip to content

Commit

Permalink
🔧 fix call in unsafe thread (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Apr 30, 2024
1 parent 7eac09f commit ef89580
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ def get_properties(self, mapping, update_entity=False, throw=False, **kwargs):

if attrs and update_entity:
self.update_attrs(attrs, update_subs=True)
self.async_write_ha_state()
self.schedule_update_ha_state()
return attrs

async def async_get_properties(self, mapping, **kwargs):
Expand Down Expand Up @@ -1888,7 +1888,7 @@ def set_miot_property(self, siid, piid, value, did=None, **kwargs):
pass
elif prop := srv.properties.get(piid):
self._state_attrs[prop.full_name] = value
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret

async def async_set_miot_property(self, siid, piid, value, did=None, **kwargs):
Expand Down Expand Up @@ -2370,7 +2370,7 @@ async def async_added_to_hass(self):
def update_from_parent(self):
self.update()
if self.platform:
self.async_write_ha_state()
self.schedule_update_ha_state()

def update(self, data=None):
attrs = self.parent_attributes
Expand Down Expand Up @@ -2407,7 +2407,7 @@ def update_attrs(self, attrs: dict, update_parent=True):
getattr(self._parent, 'update_attrs')(attrs or {}, update_parent=False)
if self.hass and self.platform:
# don't set state before added to hass
self.async_write_ha_state()
self.schedule_update_ha_state()
return self._state_attrs

def call_parent(self, method, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@ def press(self):
**self._press_kwargs,
}
if ret := self._press_action(**kws):
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def get_stream_address(self, **kwargs):
self._url_expiration = now + 60 * 4.5
if self._prop_stream_address:
self._last_url = self._prop_stream_address.from_dict(odt)
self.async_write_ha_state()
self.schedule_update_ha_state()
self.async_check_stream_address(self._last_url)
if not kwargs.get('scheduled') or self.custom_config('keep_streaming'):
self._schedule_stream_refresh()
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ async def _time_changed_cover(self, now):
self._unsub_listener_cover()
self._unsub_listener_cover = None
self._set_position = None
self.async_write_ha_state()
self.schedule_update_ha_state()
_LOGGER.debug('cover process %s: %s', self.entity_id, {
'position': self._position,
'set_position': self._set_position,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def select_source(self, source):
if source in self._keycodes:
ret = self.press_key(source)
self._attr_app_name = source
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret
if source in self.source_list:
return super().select_source(source)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ def set_native_value(self, value):
ret = self.call_parent('call_action', self._miot_action, [val])
if ret:
self._attr_native_value = val
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret
8 changes: 4 additions & 4 deletions custom_components/xiaomi_miot/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def select_option(self, option):
if act:
if ret := self.call_action(act):
self._attr_current_option = option
self.async_write_ha_state()
self.schedule_update_ha_state()
self._attr_current_option = None
return ret

Expand Down Expand Up @@ -153,7 +153,7 @@ def select_option(self, option):
ret = self.call_parent('call_action', self._miot_action, pms)
if ret:
self._attr_current_option = option
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret


Expand All @@ -168,7 +168,7 @@ def __init__(self, parent, attr, option=None):
def update(self, data=None):
super().update(data)
self._attr_current_option = self._attr_state
self.async_write_ha_state()
self.schedule_update_ha_state()

def select_option(self, option):
"""Change the selected option."""
Expand All @@ -179,7 +179,7 @@ def select_option(self, option):
}
if ret := self._select_option(option, **kws):
self._attr_current_option = option
self.async_write_ha_state()
self.schedule_update_ha_state()
return ret
raise NotImplementedError()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def turn_on(self, **kwargs):
ret = self.call_parent('call_action', self._miot_action, None if val is None else [val])
if ret:
self._state = True
self.async_write_ha_state()
self.schedule_update_ha_state()
time.sleep(0.5)
self._state = False
return ret
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def set_value(self, value):

if ret:
self._attr_native_value = value
self.async_write_ha_state()
self.schedule_update_ha_state()
time.sleep(0.5)
self._attr_native_value = ''
return ret

1 comment on commit ef89580

@al-one
Copy link
Owner Author

@al-one al-one commented on ef89580 Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.