Skip to content

Commit

Permalink
Backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Jul 26, 2022
1 parent 9c48fce commit e0ffa7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 9 additions & 0 deletions custom_components/skykettle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if DOMAIN not in hass.data: hass.data[DOMAIN] = {}
if entry.entry_id not in hass.data: hass.data[DOMAIN][entry.entry_id] = {}

# Backward compatibility
if (CONF_FRIENDLY_NAME in entry.data
and not SkyKettle.get_model_code(entry.data[CONF_FRIENDLY_NAME])
and SkyKettle.get_model_code(entry.data[CONF_FRIENDLY_NAME] + 'S')):
config = dict(entry.data.items())
config[CONF_FRIENDLY_NAME] = config[CONF_FRIENDLY_NAME] + 'S'
hass.config_entries.async_update_entry(entry, data=config)
_LOGGER.info(f"Fixed invalid model name: {config[CONF_FRIENDLY_NAME][:-1]} -> {config[CONF_FRIENDLY_NAME]}")

kettle = KettleConnection(
mac=entry.data[CONF_MAC],
key=entry.data[CONF_PASSWORD],
Expand Down
10 changes: 3 additions & 7 deletions custom_components/skykettle/skykettle.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,13 @@ class SkyKettle():
def __init__(self, model):
_LOGGER.info(f"Kettle model: {model}")
self.model = model
self.model_code = self.get_model_code(model, backward_compatibility=True)
self.model_code = self.get_model_code(model)
if not self.model_code:
raise SkyKettleError("Unknown kettle model")

@staticmethod
def get_model_code(model, backward_compatibility=False):
if model in SkyKettle.MODEL_TYPE:
return SkyKettle.MODEL_TYPE[model]
if backward_compatibility and model + 'S' in SkyKettle.MODEL_TYPE:
return SkyKettle.MODEL_TYPE[model + 'S']
return None
def get_model_code(model):
return SkyKettle.MODEL_TYPE.get(model, None)

@abstractmethod
async def command(self, command, params=[]):
Expand Down

0 comments on commit e0ffa7b

Please sign in to comment.