From 8ac5eca39a9c6d480acec60aa6cbea1121b05ad8 Mon Sep 17 00:00:00 2001 From: Sebastian Haas Date: Sat, 2 Apr 2022 17:14:44 +0200 Subject: [PATCH] isse #25: send sourcename - send sourceid and sourcename - (re-)fetch sourcelist if empty --- custom_components/hisense_tv/media_player.py | 40 +++++++++++--------- custom_components/hisense_tv/sensor.py | 2 +- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/custom_components/hisense_tv/media_player.py b/custom_components/hisense_tv/media_player.py index 8a95585..e7f4829 100644 --- a/custom_components/hisense_tv/media_player.py +++ b/custom_components/hisense_tv/media_player.py @@ -295,6 +295,16 @@ async def async_mute_volume(self, mute): def source_list(self): """List of available input sources.""" _LOGGER.debug("source_list") + if len(self._source_list) <= 1: + self._hass.async_create_task( + mqtt.async_publish( + hass=self._hass, + topic=self._out_topic( + "/remoteapp/tv/ui_service/%s/actions/sourcelist" + ), + payload="0", + ) + ) return sorted(list(self._source_list)) @property @@ -340,26 +350,18 @@ async def async_select_source(self, source): return source_dic = self._source_list.get(source) - payload = json.dumps({"sourceid": source_dic.get("sourceid")}) + payload = json.dumps( + { + "sourceid": source_dic.get("sourceid"), + "sourcename": source_dic.get("sourcename"), + } + ) await mqtt.async_publish( hass=self._hass, topic=self._out_topic("/remoteapp/tv/ui_service/%s/actions/changesource"), payload=payload, ) - async def _check_state(self): - _LOGGER.debug("_check_state: %s", self._state) - if self._state == STATE_ON: - _LOGGER.debug("_check_state skip") - return - - _LOGGER.debug("_check_state publish") - await mqtt.async_publish( - hass=self._hass, - topic=self._out_topic("/remoteapp/tv/ui_service/%s/actions/getvolume"), - payload="0", - ) - async def async_will_remove_from_hass(self): for unsubscribe in list(self._subscriptions.values()): unsubscribe() @@ -402,14 +404,18 @@ async def _message_received_turnoff(self, msg): async def _message_received_sourcelist(self, msg): """Run when new MQTT message has been received.""" - await self._check_state() + if msg.retain: + _LOGGER.debug("_message_received_sourcelist - skip retained message") + return try: payload = json.loads(msg.payload) except JSONDecodeError: payload = [] - self._source_list = {s.get("sourcename"): s for s in payload} - self._source_list["App"] = {} _LOGGER.debug("message_received_sourcelist R(%s):\n%s", msg.retain, payload) + if len(payload) > 0: + self._state = STATE_ON + self._source_list = {s.get("sourcename"): s for s in payload} + self._source_list["App"] = {} async def _message_received_volume(self, msg): """Run when new MQTT message has been received.""" diff --git a/custom_components/hisense_tv/sensor.py b/custom_components/hisense_tv/sensor.py index cab17b6..63e46e0 100644 --- a/custom_components/hisense_tv/sensor.py +++ b/custom_components/hisense_tv/sensor.py @@ -113,7 +113,7 @@ async def _message_received(self, msg): try: payload = json.loads(msg.payload) except JSONDecodeError: - payload = [] + payload = {} _LOGGER.debug("_message_received R(%s):\n%s", msg.retain, payload) self._state = { s.get("menu_id"): {"name": s.get("menu_name"), "value": s.get("menu_value")}