Skip to content

Commit

Permalink
Resolve GB restart issue (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinRobbins authored Oct 23, 2024
1 parent 8cd5f0b commit 08e7eca
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions custom_components/lghorizon/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.util import dt as dt_util
from .const import (
API,
CONF_REFRESH_TOKEN,
DOMAIN,
RECORD,
REWIND,
Expand Down Expand Up @@ -50,7 +51,7 @@ async def async_setup_entry(
players = []
api = hass.data[DOMAIN][entry.entry_id][API]
for box in api.settop_boxes.values():
players.append(LGHorizonMediaPlayer(box, api))
players.append(LGHorizonMediaPlayer(box, api, hass, entry))
async_add_entities(players, True)

platform = entity_platform.async_get_current_platform()
Expand Down Expand Up @@ -122,10 +123,12 @@ def device_info(self):
"model": self._box.model or "unknown",
}

def __init__(self, box: LGHorizonBox, api: LGHorizonApi):
def __init__(self, box: LGHorizonBox, api: LGHorizonApi, hass: HomeAssistant, entry: ConfigEntry):
"""Init the media player."""
self._box = box
self.api = api
self.hass = hass
self.entry = entry
self.box_id = box.deviceId
self.box_name = box.deviceFriendlyName
self._create_channel_map()
Expand All @@ -141,7 +144,22 @@ async def async_added_to_hass(self):
def callback(box_id):
self.schedule_update_ha_state(True)

def refresh_callback():
self.hass.add_job(self._save_refresh_token)

self._box.set_callback(callback)
self.api.set_callback(refresh_callback)

@callback
def _save_refresh_token(self):
"""Save the refresh token."""
if CONF_REFRESH_TOKEN in self.entry.data:
_LOGGER.info("New JWT stored (2): %s", self.api.refresh_token)
new_data = {**self.entry.data}
new_data[CONF_REFRESH_TOKEN] = self.api.refresh_token
self.hass.config_entries.async_update_entry(
self.entry, data=new_data
)

async def async_update(self):
"""Update the box."""
Expand Down

0 comments on commit 08e7eca

Please sign in to comment.