From b1ea32e39f366e76425b4bdec0ed2a3cb165b863 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 7 Apr 2024 06:48:30 +0300 Subject: [PATCH] Code refactoring --- .github/workflows/hacs.yml | 2 +- .gitignore | 3 +- custom_components/dash_cast/__init__.py | 45 ++++++++++++++-------- custom_components/dash_cast/config_flow.py | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/.github/workflows/hacs.yml b/.github/workflows/hacs.yml index 765435a..df4d261 100644 --- a/.github/workflows/hacs.yml +++ b/.github/workflows/hacs.yml @@ -10,7 +10,7 @@ jobs: steps: - uses: "actions/checkout@v2" - uses: "hacs/action@main" - with: { category: "integration" } + with: { category: "integration", ignore: "brands" } hassfest: runs-on: "ubuntu-latest" steps: diff --git a/.gitignore b/.gitignore index 3ae877c..44866c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ - +__pycache__/ .idea/ - .homeassistant/ diff --git a/custom_components/dash_cast/__init__.py b/custom_components/dash_cast/__init__.py index 5965ae7..13a66c5 100644 --- a/custom_components/dash_cast/__init__.py +++ b/custom_components/dash_cast/__init__.py @@ -1,33 +1,46 @@ -from homeassistant.components.media_player import DOMAIN as MP_DOMAIN +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID -from homeassistant.core import ServiceCall +from homeassistant.core import ServiceCall, HomeAssistant +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_component import DATA_INSTANCES - +from pychromecast import Chromecast from pychromecast.controllers.dashcast import DashCastController -DOMAIN = 'dash_cast' +DOMAIN = "dash_cast" + +CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) -async def async_setup(hass, hass_config): +async def async_setup(hass: HomeAssistant, config: dict) -> bool: dashs = {} async def play_media(call: ServiceCall): entity_ids = call.data.get(ATTR_ENTITY_ID) - url = call.data.get('url') - force = call.data.get('force', False) - - for entity in hass.data[DATA_INSTANCES][MP_DOMAIN].entities: - if entity.entity_id in entity_ids: - dash = dashs.get(entity.entity_id) - if not dash: + kwargs = { + k: v + for k, v in call.data.items() + if k in ("url", "force", "reload_seconds") + } + + for entity in hass.data[DATA_INSTANCES]["media_player"].entities: + if entity.entity_id not in entity_ids: + continue + + dash: DashCastController = dashs.get(entity.entity_id) + if not dash: + chromecast: Chromecast = getattr(entity, "_chromecast") + if chromecast: dashs[entity.entity_id] = dash = DashCastController() - entity._chromecast.register_handler(dash) - dash.load_url(url, force=force) + chromecast.register_handler(dash) + else: + return + + dash.load_url(**kwargs) - hass.services.async_register(DOMAIN, 'load_url', play_media) + hass.services.async_register(DOMAIN, "load_url", play_media) return True -async def async_setup_entry(hass, entry): +async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: return True diff --git a/custom_components/dash_cast/config_flow.py b/custom_components/dash_cast/config_flow.py index b173778..5b5e699 100644 --- a/custom_components/dash_cast/config_flow.py +++ b/custom_components/dash_cast/config_flow.py @@ -9,5 +9,5 @@ async def async_step_import(self, user_input=None): async def async_step_user(self, user_input=None): if self._async_current_entries(): - return self.async_abort(reason='single_instance_allowed') + return self.async_abort(reason="single_instance_allowed") return self.async_create_entry(title="DashCast", data={})