Skip to content

Commit

Permalink
DashCast code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 7, 2024
1 parent 309b6d5 commit 031756e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def dash_cast(call: ServiceCallType):
hass,
call.data[ATTR_ENTITY_ID],
f"{get_url(hass)}/webrtc/embed?" + urlencode(query),
call.data.get("force"),
call.data.get("force", False),
)

hass.services.async_register(DOMAIN, "create_link", create_link, CREATE_LINK_SCHEMA)
Expand Down
23 changes: 9 additions & 14 deletions custom_components/webrtc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,24 @@ async def init_resource(hass: HomeAssistant, url: str, ver: str) -> bool:


# noinspection PyProtectedMember
def dash_cast(hass: HomeAssistant, cast_entities: list, url: str, force=False):
def dash_cast(hass: HomeAssistant, entities: list, url: str, force: bool):
"""Cast webpage to chromecast device via DashCast application."""
try:
entities = [
e
for e in hass.data[DATA_INSTANCES]["media_player"].entities
if e.entity_id in cast_entities and getattr(e, "_chromecast", 0)
]
if not entities:
_LOGGER.warning(f"Can't find {cast_entities} for DashCast")

for entity in entities:
from pychromecast.controllers.dashcast import DashCastController
for entity in hass.data[DATA_INSTANCES]["media_player"].entities:
if entity.entity_id not in entities or not hasattr(entity, "_chromecast"):
continue

if not hasattr(entity, "dashcast"):
from pychromecast.controllers.dashcast import DashCastController

entity.dashcast = DashCastController()
entity._chromecast.register_handler(entity.dashcast)

_LOGGER.debug(f"DashCast to {entity.entity_id}")
entity.dashcast.load_url(url, force = force)
entity.dashcast.load_url(url, force=force)

except Exception:
_LOGGER.exception(f"Can't DashCast to {cast_entities}")
except Exception as e:
_LOGGER.error(f"Can't DashCast to {entities}", exc_info=e)


def validate_signed_request(request: web.Request) -> bool:
Expand Down

0 comments on commit 031756e

Please sign in to comment.