Skip to content

Commit

Permalink
feat: remove update routine for 1.2.4->1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
IATkachenko committed Sep 3, 2021
1 parent dafc1a2 commit 37d2edb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 84 deletions.
50 changes: 0 additions & 50 deletions custom_components/sleep_as_android/__init__.py
Expand Up @@ -43,12 +43,6 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, registry: er)
except KeyError:
self._name = 'SleepAsAndroid'

try:
_tt = self._config_entry.options['topic_template']
self._updating = False
except KeyError:
self._updating = True

# will call async_setup_entry from sensor.py
self.hass.loop.create_task(self.hass.config_entries.async_forward_entry_setup(self._config_entry, 'sensor'))
# ToDo prepare topic_template and other variables that should be defined one time.
Expand Down Expand Up @@ -216,47 +210,3 @@ def get_sensor(self, sensor_name: str) -> (SleepAsAndroidSensor, bool):
new_sensor = SleepAsAndroidSensor(self.hass, self._config_entry, sensor_name)
self.__sensors[sensor_name] = new_sensor
return new_sensor, True

@property
def updating(self) -> bool:
return self._updating

def self_update(self):
"""
Run updating routine
"""
old_options = self._config_entry.options
_LOGGER.debug(f"old options is {old_options}")
try:
new_options = {
'name': old_options['name'],
'qos': old_options['qos']
}
except KeyError:
_LOGGER.debug("old options was not found. Will use defaults")
new_options = {
'name': self._name,
'qos': 0,
}
try:
topic = old_options['root_topic']
new_topic = topic + "/" + DEVICE_MACRO
_LOGGER.info(f"Found root_topic {topic} from v1.2.4. Will replace it by {new_topic}")
except KeyError:
_LOGGER.info(f"root_topic for v1.2.4 not found. Will try 'topic' from v1.1.0")
try:
topic = old_options['topic'] # full topic for message. Should cut after last /
new_topic = '/'.join(topic.split('/')[:-1]) + "/" + DEVICE_MACRO
_LOGGER.info(f"Found topic '{topic}' from v1.1.0. Will replace it by {new_topic}")
except KeyError:
new_topic = self.configured_topic
_LOGGER.info(f"No topic information from previous versions found. Will use {new_topic}")

new_options['topic_template'] = new_topic
_LOGGER.info("Updating...")
self.hass.config_entries.async_update_entry(
self._config_entry,
options=new_options
)
_LOGGER.info("Done!")
self._updating = False
34 changes: 0 additions & 34 deletions custom_components/sleep_as_android/sensor.py
Expand Up @@ -34,30 +34,15 @@ async def add_configured_entities():
for entity in entities:

device_name = instance.device_name_from_entity_id(entity.unique_id)
# If we in update mode, then remove instance_name prefix from device name
if instance.updating:
_LOGGER.info(f"We in updating mode. Removing prefix {instance.name} for {device_name}")
device_name = device_name.removeprefix(instance.name+"_")
instance.entity_registry.async_update_entity(
entity_id=entity.entity_id,
new_unique_id=instance.create_entity_id(device_name)
)
_LOGGER.debug(f"add_configured_entities: creating sensor with name {device_name}")
(sensor, _) = instance.get_sensor(device_name)
if instance.updating:
await sensor.device_update()
sensors.append(sensor)

async_add_entities(sensors)

instance: SleepAsAndroidInstance = hass.data[DOMAIN][config_entry.entry_id]
await add_configured_entities()
_LOGGER.debug("async_setup_entry: adding configured entities is finished.")

if instance.updating:
_LOGGER.debug("We need update. Running self_update")
instance.self_update()

_LOGGER.debug("Going to subscribe to root topic.")
await instance.subscribe_root_topic(async_add_entities)
_LOGGER.debug("async_setup_entry is finished")
Expand Down Expand Up @@ -163,22 +148,3 @@ def device_info(self):
info = {"identifiers": {(DOMAIN, self.unique_id)}, "name": self.name, "manufacturer": "SleepAsAndroid",
"type": None, "model": "MQTT"}
return info

async def device_update(self) -> None:
if not self._instance.updating:
return

device_registry = await dr.async_get_registry(self.hass)
# Instance in updating mode. Device should be update too
_LOGGER.info("Updating device identifiers")
# v1.2.4 -> v1.2.5 update
old_identifiers = {(DOMAIN, self._instance.name + "_" + self.unique_id)}
_LOGGER.info(f"Trying to get v1.2.4 device {old_identifiers}")
device = device_registry.async_get_device(identifiers=old_identifiers, connections=set())
if device is not None:
_LOGGER.info(f"Found device {device.id} from v1.2.4. Updating ... ")
device_registry.async_update_device(
device_id=device.id,
new_identifiers=self.device_info['identifiers']
)
_LOGGER.info("Device updating finished")

0 comments on commit 37d2edb

Please sign in to comment.