diff --git a/cmdserver/infoprovider.py b/cmdserver/infoprovider.py index a979cc4..9af7c5e 100644 --- a/cmdserver/infoprovider.py +++ b/cmdserver/infoprovider.py @@ -50,7 +50,7 @@ def __init__(self, config: Config, ws_server: WsServer, mqtt: Optional[MQTT]): 'port': self.__ms.port, 'token': '', 'ssl': False, - 'alive': True + 'alive': False }, 'zones': {} } @@ -61,7 +61,7 @@ def __init__(self, config: Config, ws_server: WsServer, mqtt: Optional[MQTT]): self.__mqtt.offline(self.__mqtt_name) def __state_to_str(self) -> str: - return json.dumps(self.__current_state, ensure_ascii=False) + return json.dumps(self.info, ensure_ascii=False) @property def info(self): @@ -91,7 +91,8 @@ def __async_refresh(self): else: zd = self.__zone_to_dict(z, None) zones_data[z.id] = zd - playing = self.__current_state.get('playingCommand', {}) + playing = self.info.get('playingCommand', {}) + was_alive = self.info['config']['alive'] self.__current_state = { 'config': { 'host': self.__ms.local_ip, @@ -104,11 +105,12 @@ def __async_refresh(self): 'playingCommand': playing } last_active_zone_id = playing.get('id', '') - playing['active'] = self.get_active_command(self.__current_state['zones'].get(active_zone.id, None)) + playing['active'] = self.get_active_command(zones_data.get(active_zone.id, None)) if active_zone.id: playing['id'] = active_zone.id self.__ws_server.broadcast(self.__state_to_str()) - self.__broadcast_up() + if not was_alive: + self.__mqtt.online(self.__mqtt_name) if active_zone.id != last_active_zone_id: self.__mqtt.publish(self.__mqtt_name, json.dumps({'zone': active_zone.id})) except ConnectTimeout as e: @@ -133,15 +135,8 @@ def __async_refresh(self): logger.exception(f"Unexpected failure to refresh current state of {self.__ms.address()}") self.__broadcast_down() - def __broadcast_up(self): - was_alive = not self.__current_state or self.__current_state.get('config', {}).get('alive', True) is True - if not was_alive: - logger.warning(f"Broadcasting MC is UP") - if self.__mqtt: - self.__mqtt.online(self.__mqtt_name) - def __broadcast_down(self): - was_alive = not self.__current_state or self.__current_state.get('config', {}).get('alive', True) is True + was_alive = self.__current_state['config']['alive'] is True if was_alive: logger.warning(f"Broadcasting MC is DOWN") self.__current_state = { @@ -153,7 +148,7 @@ def __broadcast_down(self): 'alive': False }, 'zones': self.__current_state['zones'] if self.__current_state else {}, - 'playingCommand': {'active': DEFAULT_ACTIVE_COMMAND} + 'playingCommand': {'active': DEFAULT_ACTIVE_COMMAND, 'id': ''} } self.__ws_server.broadcast(self.__state_to_str()) if self.__mqtt: diff --git a/pyproject.toml b/pyproject.toml index f557c77..a3e6845 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ezmote-cmdserver" -version = "1.1.11" +version = "1.1.12" description = "A small webapp which can be used for web based home cinema automation" authors = ["3ll3d00d "] license = "MIT"