fix: fix for slow init

This commit is contained in:
Luke Bonaccorsi 2024-03-06 18:42:42 +00:00
parent e2d9d22ca6
commit 55ef025e5e
2 changed files with 42 additions and 17 deletions

View File

@ -761,7 +761,7 @@ class TuyaDevice:
message = Message(Message.GET_COMMAND, payload, encrypt=encrypt, device=self) message = Message(Message.GET_COMMAND, payload, encrypt=encrypt, device=self)
self._queue.append(message) self._queue.append(message)
response = await self.async_recieve(message) response = await self.async_recieve(message)
asyncio.create_task(self.async_update_state(response)) await self.async_update_state(response)
async def async_set(self, dps): async def async_set(self, dps):
t = int(time.time()) t = int(time.time())
@ -903,9 +903,6 @@ class TuyaDevice:
await self._async_send(message, retries=retries - 1) await self._async_send(message, retries=retries - 1)
async def async_recieve(self, message): async def async_recieve(self, message):
if self._connected is False:
return
if message.expect_response is True: if message.expect_response is True:
try: try:
self._recieve_task = asyncio.create_task( self._recieve_task = asyncio.create_task(

View File

@ -266,7 +266,15 @@ class RoboVacEntity(StateVacuumEntity):
self._attr_supported_features = self.vacuum.getHomeAssistantFeatures() self._attr_supported_features = self.vacuum.getHomeAssistantFeatures()
self._attr_robovac_supported = self.vacuum.getRoboVacFeatures() self._attr_robovac_supported = self.vacuum.getRoboVacFeatures()
self._attr_fan_speed_list = self.vacuum.getFanSpeeds()
fan_speeds = self.vacuum.getFanSpeeds()
self.fan_speed_map = {}
for speed in fan_speeds:
self.fan_speed_map[friendly_text(speed)] = speed
self._attr_fan_speed_list = list(self.fan_speed_map.keys())
_LOGGER.debug(self._attr_fan_speed_list)
self._tuya_command_codes = self.vacuum.getCommandCodes() self._tuya_command_codes = self.vacuum.getCommandCodes()
self._attr_mode = None self._attr_mode = None
@ -285,19 +293,14 @@ class RoboVacEntity(StateVacuumEntity):
self.tuya_state = None self.tuya_state = None
self.tuyastatus = None self.tuyastatus = None
async def async_added_to_hass(self):
await self.async_forced_update()
async def async_update(self): async def async_update(self):
"""Synchronise state from the vacuum.""" """Synchronise state from the vacuum."""
if self.error_code == "UNSUPPORTED_MODEL":
return
if self.ip_address == "":
self.error_code = "IP_ADDRESS"
return
try: try:
await self.vacuum.async_get() await self.async_update_vacuum()
self.update_failures = 0 self.update_failures = 0
self.update_entity_values()
except TuyaException as e: except TuyaException as e:
self.update_failures += 1 self.update_failures += 1
_LOGGER.warn( _LOGGER.warn(
@ -308,6 +311,21 @@ class RoboVacEntity(StateVacuumEntity):
if self.update_failures >= UPDATE_RETRIES: if self.update_failures >= UPDATE_RETRIES:
self.error_code = "CONNECTION_FAILED" self.error_code = "CONNECTION_FAILED"
async def async_update_vacuum(self):
if self.error_code == "UNSUPPORTED_MODEL":
return
if self.ip_address == "":
self.error_code = "IP_ADDRESS"
return
await self.vacuum.async_get()
self.update_entity_values()
async def async_forced_update(self):
await self.async_update_vacuum()
self.async_write_ha_state()
async def pushed_update_handler(self): async def pushed_update_handler(self):
self.update_entity_values() self.update_entity_values()
self.async_write_ha_state() self.async_write_ha_state()
@ -327,8 +345,8 @@ class RoboVacEntity(StateVacuumEntity):
self._attr_mode = self.tuyastatus.get( self._attr_mode = self.tuyastatus.get(
self._tuya_command_codes[RobovacCommand.MODE] self._tuya_command_codes[RobovacCommand.MODE]
) )
self._attr_fan_speed = self.tuyastatus.get( self._attr_fan_speed = friendly_text(
self._tuya_command_codes[RobovacCommand.FAN_SPEED] self.tuyastatus.get(self._tuya_command_codes[RobovacCommand.FAN_SPEED], "")
) )
if self.robovac_supported & RoboVacEntityFeature.CLEANING_AREA: if self.robovac_supported & RoboVacEntityFeature.CLEANING_AREA:
@ -414,7 +432,11 @@ class RoboVacEntity(StateVacuumEntity):
"""Set fan speed.""" """Set fan speed."""
_LOGGER.info("Fan Speed Selected") _LOGGER.info("Fan Speed Selected")
await self.vacuum.async_set( await self.vacuum.async_set(
{self._tuya_command_codes[RobovacCommand.FAN_SPEED]: fan_speed} {
self._tuya_command_codes[RobovacCommand.FAN_SPEED]: self.fan_speed_map[
fan_speed
]
}
) )
async def async_send_command( async def async_send_command(
@ -461,3 +483,9 @@ class RoboVacEntity(StateVacuumEntity):
async def async_will_remove_from_hass(self): async def async_will_remove_from_hass(self):
await self.vacuum.async_disable() await self.vacuum.async_disable()
def friendly_text(input):
return " ".join(
word[0].upper() + word[1:] for word in input.replace("_", " ").split()
)