Skip to content

Commit f46a955

Browse files
committed
Option to filter lights that are already on. Do not filter by default
1 parent db253d0 commit f46a955

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ media_player:
2020
- platform: lightcast
2121
name: Living Room LightCast
2222
target: area.living_room
23+
# Enable filter_on to only consider lights that are already on
24+
# filter_on: false
2325
```
2426

2527
Target can be a light, area, group or device.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONF_TARGET = 'target'
22
CONF_NAME = 'name'
3+
CONF_FILTER_ON = 'filter_on'

custom_components/lightcast/media_player.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async def async_setup_platform(
3535
hass,
3636
config[const.CONF_NAME],
3737
config[const.CONF_TARGET],
38+
config.get(const.CONF_FILTER_ON) or False
3839
)
3940

4041
add_entities([cast_device])
@@ -45,10 +46,11 @@ class LightCastPlayer(MediaPlayerEntity):
4546
_attr_supported_features = MediaPlayerEntityFeature.PLAY_MEDIA | MediaPlayerEntityFeature.BROWSE_MEDIA
4647
_attr_device_class = MediaPlayerDeviceClass.TV
4748

48-
def __init__(self, hass: HomeAssistant, name: str, device_target: str) -> None:
49+
def __init__(self, hass: HomeAssistant, name: str, device_target: str, filter_on: bool) -> None:
4950
self.hass = hass
50-
self.device_target = device_target
5151
self._attr_name = name
52+
self.device_target = device_target
53+
self.filter_on = filter_on
5254

5355
async def async_browse_media(self, media_content_type: str | None = None,
5456
media_content_id: str | None = None) -> BrowseMedia:
@@ -78,13 +80,12 @@ async def process_image(self, media_type: str, media_id: str) -> None:
7880
found_entities = expand_entities(self.hass, self.device_target)
7981
_LOGGER.info('Found %d entities matching %s', len(found_entities), self.device_target)
8082

81-
if not found_entities:
82-
_LOGGER.warning('No entities were matched')
83-
return
83+
valid_entities = found_entities
84+
if self.filter_on:
85+
valid_entities = [e for e in found_entities if e.state == 'on']
8486

85-
valid_entities = [e for e in found_entities if e.state == 'on']
8687
if not valid_entities:
87-
_LOGGER.warning('No targets are on')
88+
_LOGGER.warning('No entities were matched')
8889
return
8990

9091
async with aiohttp.ClientSession() as session:

0 commit comments

Comments
 (0)