Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 7, 2024
1 parent be28f65 commit b1ea32e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hacs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

__pycache__/
.idea/

.homeassistant/
45 changes: 29 additions & 16 deletions custom_components/dash_cast/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion custom_components/dash_cast/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={})

0 comments on commit b1ea32e

Please sign in to comment.