Skip to content

Commit

Permalink
Merge pull request #36 from Garulf/add-scene-support
Browse files Browse the repository at this point in the history
Add scene support
  • Loading branch information
Garulf authored Oct 27, 2022
2 parents baf46fd + 49ad9a4 commit f341775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"Name": "HA-Commander",
"Description": "Search, and interact with Home Assistant using Wox or Flow Launcher.",
"Author": "Garulf",
"Version": "4.0.0",
"Version": "5.0.0",
"Language": "python",
"Website": "https://github.com/Garulf/HA-Commander",
"IcoPath": "icons\\home-assistant.png",
"ExecuteFileName": "main.py"
}
}
37 changes: 29 additions & 8 deletions plugin/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def __init__(self, client, entity):
self.entity_id = entity.get("entity_id")
self.domain = self.entity_id.split(".")[0]
self.name = entity.get("name")
self.friendly_name = entity.get("attributes", "").get("friendly_name", "")
self.friendly_name = entity.get(
"attributes", "").get("friendly_name", "")
self.state = entity.get("state")
self.attributes = entity["attributes"]
self.target = {"entity_id": self.entity_id}
Expand Down Expand Up @@ -225,15 +226,17 @@ def turn_on(self, **service_data) -> any:
for arg in service_data:
service_data[arg] = service_data[arg]
service_data["entity_id"] = self.entity_id
self._client.call_services("homeassistant", "turn_on", data=service_data)
self._client.call_services(
"homeassistant", "turn_on", data=service_data)

@service(icon="switch_off", score=100)
def turn_off(self, **service_data) -> None:
"""Turn entity off."""
for arg in service_data:
service_data[arg] = service_data[arg]
service_data["entity_id"] = self.entity_id
self._client.call_services("homeassistant", "turn_off", data=service_data)
self._client.call_services(
"homeassistant", "turn_off", data=service_data)


class Light(Entity):
Expand All @@ -250,7 +253,8 @@ def __init__(self, client: Client, entity: dict) -> None:
for effect in self.attributes.get("effect_list", []):
setattr(self, f"{effect}", partial(self.turn_on, effect=effect))
getattr(self, f"{effect}").name = effect.title()
getattr(self, f"{effect}").__doc__ = f"Set light effect to {effect}."
getattr(
self, f"{effect}").__doc__ = f"Set light effect to {effect}."
getattr(self, f"{effect}").icon = "star-circle-outline"
getattr(self, f"{effect}").score = 0
# print(getattr(self, f"{color}").__icon__)
Expand Down Expand Up @@ -336,12 +340,14 @@ def play(self) -> None:
@service(icon="pause")
def pause(self) -> None:
"""Pause currently playing media."""
self._client.call_services("media_player", "media_pause", data=self.target)
self._client.call_services(
"media_player", "media_pause", data=self.target)

@service(icon="play-pause")
def play_pause(self) -> None:
"""Toggle Play/Pause."""
self._client.call_services("media_player", "media_play_pause", data=self.target)
self._client.call_services(
"media_player", "media_play_pause", data=self.target)


class Climate(Entity):
Expand All @@ -362,7 +368,8 @@ def cycle_mode(self) -> None:
mode_index = 0
service_data = self.target
service_data["hvac_mode"] = self.hvac_modes[mode_index]
self._client.call_services("climate", "set_hvac_mode", data=service_data)
self._client.call_services(
"climate", "set_hvac_mode", data=service_data)


class Script(Entity):
Expand Down Expand Up @@ -413,7 +420,8 @@ def snapshot(self) -> None:
@service(icon="television")
def view(self) -> None:
"""View a still from this Camera entity."""
webbrowser.open(f'{self._client._url}{self.attributes["entity_picture"]}')
webbrowser.open(
f'{self._client._url}{self.attributes["entity_picture"]}')


class InputSelect(BaseEntity):
Expand Down Expand Up @@ -467,3 +475,16 @@ def __init__(self, client: Client, entity: dict) -> None:
getattr(self, entity).__doc__ = 'Toggle entity "{}"'.format(entity)
getattr(self, entity).icon = "checkbox-multiple-blank"
getattr(self, entity)._service = True


class Scene(Entity):
def __init__(self, client: Client, entity: dict) -> None:
super().__init__(client, entity)

def _default_action(self):
self.activate()

@service(icon="play")
def activate(self) -> None:
"""Activate scene."""
self._client.call_services("scene", "turn_on", data=self.target)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
requests==2.25.1
flox-lib==0.18.0
flox-lib==0.18.1

0 comments on commit f341775

Please sign in to comment.