Skip to content

Commit

Permalink
isse #25: send sourcename
Browse files Browse the repository at this point in the history
- send sourceid and sourcename
- (re-)fetch sourcelist if empty
  • Loading branch information
sehaas committed Apr 2, 2022
1 parent 1a2e519 commit 8ac5eca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
40 changes: 23 additions & 17 deletions custom_components/hisense_tv/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hisense_tv/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down

0 comments on commit 8ac5eca

Please sign in to comment.