Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions homeassistant/components/broadlink/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
)
from homeassistant.const import CONF_HOST, STATE_OFF
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.storage import Store
Expand Down Expand Up @@ -110,12 +109,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
Store(hass, CODE_STORAGE_VERSION, f"broadlink_remote_{device.unique_id}_codes"),
Store(hass, FLAG_STORAGE_VERSION, f"broadlink_remote_{device.unique_id}_flags"),
)

loaded = await remote.async_load_storage_files()
if not loaded:
_LOGGER.error("Failed to create '%s Remote' entity: Storage error", device.name)
return

async_add_entities([remote], False)


Expand All @@ -128,6 +121,7 @@ def __init__(self, device, codes, flags):
self._coordinator = device.update_manager.coordinator
self._code_storage = codes
self._flag_storage = flags
self._storage_loaded = False
self._codes = {}
self._flags = defaultdict(int)
self._state = True
Expand Down Expand Up @@ -214,12 +208,12 @@ def _extract_codes(self, commands, device=None):
return code_list

@callback
def get_codes(self):
def _get_codes(self):
"""Return a dictionary of codes."""
return self._codes

@callback
def get_flags(self):
def _get_flags(self):
"""Return a dictionary of toggle flags.

A toggle flag indicates whether the remote should send an
Expand Down Expand Up @@ -250,16 +244,13 @@ async def async_turn_off(self, **kwargs):
self._state = False
self.async_write_ha_state()

async def async_load_storage_files(self):
"""Load codes and toggle flags from storage files."""
try:
self._codes.update(await self._code_storage.async_load() or {})
self._flags.update(await self._flag_storage.async_load() or {})

except HomeAssistantError:
return False

return True
async def _async_load_storage(self):
"""Load code and flag storage from disk."""
# Exception is intentionally not trapped to
# provide feedback if something fails.
self._codes.update(await self._code_storage.async_load() or {})
self._flags.update(await self._flag_storage.async_load() or {})
self._storage_loaded = True

async def async_send_command(self, command, **kwargs):
"""Send a list of commands to a device."""
Expand All @@ -277,6 +268,9 @@ async def async_send_command(self, command, **kwargs):
)
return

if not self._storage_loaded:
await self._async_load_storage()

try:
code_list = self._extract_codes(commands, device)
except ValueError as err:
Expand Down Expand Up @@ -312,7 +306,7 @@ async def async_send_command(self, command, **kwargs):
at_least_one_sent = True

if at_least_one_sent:
self._flag_storage.async_delay_save(self.get_flags, FLAG_SAVE_DELAY)
self._flag_storage.async_delay_save(self._get_flags, FLAG_SAVE_DELAY)

async def async_learn_command(self, **kwargs):
"""Learn a list of commands from a remote."""
Expand All @@ -329,6 +323,9 @@ async def async_learn_command(self, **kwargs):
)
return

if not self._storage_loaded:
await self._async_load_storage()

async with self._lock:
if command_type == COMMAND_TYPE_IR:
learn_command = self._async_learn_ir_command
Expand Down Expand Up @@ -486,6 +483,9 @@ async def async_delete_command(self, **kwargs):
)
return

if not self._storage_loaded:
await self._async_load_storage()

try:
codes = self._codes[device]
except KeyError as err:
Expand Down Expand Up @@ -516,6 +516,6 @@ async def async_delete_command(self, **kwargs):
if not codes:
del self._codes[device]
if self._flags.pop(device, None) is not None:
self._flag_storage.async_delay_save(self.get_flags, FLAG_SAVE_DELAY)
self._flag_storage.async_delay_save(self._get_flags, FLAG_SAVE_DELAY)

self._code_storage.async_delay_save(self.get_codes, CODE_SAVE_DELAY)
self._code_storage.async_delay_save(self._get_codes, CODE_SAVE_DELAY)