Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic open/close handler (former cover) and HomeAssistant GUI loader #91

Merged
merged 18 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Currently the following entity types are supported: `light`, `switch`, `scene`,
* "Turn off bedroom lights"
* "Turn on on the AC"
* "Read bedroom temperature"
* "Open the living room blinds"
* "Close the garage door"
* "Open Home Assistant"
* "What is the state of power plug bedroom"
* "Has water sensor detected moisture"

## Credits
@BongoEADGC6
Expand Down Expand Up @@ -68,7 +73,7 @@ def handle_lighting_intent(self, message):
ha_entity = self.ha.find_entity(entity, ['group','light', 'switch', 'scene', 'input_boolean'])
if ha_entity is None:
#self.speak("Sorry, I can't find the Home Assistant entity %s" % entity)
self.speak_dialog('homeassistant.device.unknown', data={"dev_name": ha_entity['dev_name']})
self.speak_dialog('homeassistant.error.device.unknown', data={"dev_name": ha_entity['dev_name']})
return
ha_data = {'entity_id': ha_entity['id']}
```
Expand Down
113 changes: 78 additions & 35 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _find_entity(self, entity, domains):
ha_entity = self._handle_client_exception(self.ha_client.find_entity,
entity, domains)
if ha_entity is None:
self.speak_dialog('homeassistant.device.unknown', data={
self.speak_dialog('homeassistant.error.device.unknown', data={
"dev_name": entity})
return ha_entity

Expand All @@ -125,7 +125,7 @@ def _check_availability(self, ha_entity):
if ha_entity['state'] == 'unavailable':
"""Check if state is unavailable, if yes, inform user about it."""

self.speak_dialog('homeassistant.device.unavailable', data={
self.speak_dialog('homeassistant.error.device.unavailable', data={
"dev_name": ha_entity['dev_name']})
"""Return result to underlining function."""
return False
Expand Down Expand Up @@ -178,26 +178,26 @@ def handle_turn_off_intent(self, message):
message.data["Action"] = "off"
self._handle_turn_actions(message)

@intent_handler('cover.open.intent')
def handle_open_cover(self, message):
"""Handle open cover intent."""
@intent_handler('open.intent')
def handle_open(self, message):
"""Handle open intent."""
message.data["Entity"] = message.data.get("entity")
message.data["Action"] = "open_cover"
self._handle_cover_actions(message)
message.data["Action"] = "open"
self._handle_open_close_actions(message)

@intent_handler('cover.close.intent')
def handle_close_cover(self, message):
"""Handle close cover intent."""
@intent_handler('close.intent')
def handle_close(self, message):
"""Handle close intent."""
message.data["Entity"] = message.data.get("entity")
message.data["Action"] = "close_cover"
self._handle_cover_actions(message)
message.data["Action"] = "close"
self._handle_open_close_actions(message)

@intent_handler('cover.stop.intent')
def handle_stop_cover(self, message):
"""Handle stop cover intent."""
@intent_handler('stop.intent')
def handle_stop(self, message):
"""Handle stop intent."""
message.data["Entity"] = message.data.get("entity")
message.data["Action"] = "stop_cover"
self._handle_cover_actions(message)
message.data["Action"] = "stop"
self._handle_stop_actions(message)

@intent_handler('toggle.intent')
def handle_toggle_intent(self, message):
Expand Down Expand Up @@ -387,33 +387,75 @@ def _handle_shopping_list(self, message):
self.ha_client.execute_service("shopping_list", "add_item", ha_data)
self.speak_dialog("homeassistant.shopping.list")

def _handle_cover_actions(self, message):
"""Handler for cover open, close and stop action."""
def _handle_open_close_actions(self, message):
"""Handler for open and close actions."""
entity = message.data["Entity"]
action = message.data["Action"]

if self.voc_match(entity, "HomeAssistant"):
if not self.gui.connected:
self.speak_dialog('homeassistant.error.no_gui')
return

if action == "open":
self.acknowledge()
self.gui.clear()
self.gui.show_url(self.ha_client.url, override_idle=True)
elif action == "close":
self.acknowledge()
self.gui.release()
return

ha_entity = self._find_entity(entity, ['cover'])
# Exit if entity not found or is unavailabe
if not ha_entity or not self._check_availability(ha_entity):
return

entity = ha_entity['id']
domain = entity.split(".")[0]

ha_data = {'entity_id': ha_entity['id']}

response = self.ha_client.execute_service("cover", action, ha_data)
if domain == "cover":
response = self.ha_client.execute_service("cover", f"{action}_cover", ha_data)

if response.status_code != 200:
return

if action == "open":
self.speak_dialog("homeassistant.device.opening",
data=ha_entity)
elif action == "close":
self.speak_dialog("homeassistant.device.closing",
data=ha_entity)
return

return

if response.status_code != 200:
def _handle_stop_actions(self, message):
"""Handler for stop actions."""
entity = message.data["Entity"]

ha_entity = self._find_entity(entity, ['cover'])
# Exit if entity not found or is unavailabe
if not ha_entity or not self._check_availability(ha_entity):
return

entity = ha_entity['id']
domain = entity.split(".")[0]

ha_data = {'entity_id': ha_entity['id']}

if domain == "cover":
response = self.ha_client.execute_service("cover", "stop_cover", ha_data)

if response.status_code != 200:
return

self.speak_dialog("homeassistant.device.stopped",
data=ha_entity)
return

if action == "open_cover":
self.speak_dialog("homeassistant.sensor.cover.opening",
data=ha_entity
)
elif action == "close_cover":
self.speak_dialog("homeassistant.sensor.cover.closing",
data=ha_entity
)
elif action == "stop_cover":
self.speak_dialog("homeassistant.sensor.cover.stopped",
data=ha_entity
)
return

def _handle_light_adjust(self, message):
Expand Down Expand Up @@ -581,10 +623,11 @@ def _handle_sensor(self, message):
"current_temp": current_temp,
"targeted_temp": target_temp})
elif domain == "cover":
self.speak_dialog(f'homeassistant.sensor.cover.{sensor_state}', data={
self.speak_dialog(f'homeassistant.device.{sensor_state}', data={
"dev_name": sensor_name})
elif domain == "binary_sensor":
sensor_states = self.translate_namedvalues(f'homeassistant.binary_sensor.{sensor_state}')
sensor_states = self.translate_namedvalues(
f'homeassistant.binary_sensor.{sensor_state}')
sensor_state = sensor_states['default']

if attributes.get('device_class') in sensor_states:
Expand Down
4 changes: 1 addition & 3 deletions dialog/cs-cz/homeassistant.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Omlouvám se, nepovedlo se mi najít {dev_name} v Home Assistentu.
Entita {dev_name} nebyla nalezena.
Omluvte mne, nedokázal jsem nalézt {dev_name}.
stav {dev_name} je neznámí.
3 changes: 3 additions & 0 deletions dialog/cs-cz/homeassistant.error.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Omlouvám se, nepovedlo se mi najít {dev_name} v Home Assistentu.
Entita {dev_name} nebyla nalezena.
Omluvte mne, nedokázal jsem nalézt {dev_name}.
2 changes: 2 additions & 0 deletions dialog/cs-cz/homeassistant.error.no_gui.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Omlouvám se, tento příkaz může být použit pouze s připojeným displejem.
Omlouvám se, tento příkaz funguje pouze se zapojeným displejem.
1 change: 0 additions & 1 deletion dialog/cs-cz/homeassistant.sensor.cover.unknown.dialog

This file was deleted.

5 changes: 1 addition & 4 deletions dialog/de-de/homeassistant.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Entschuldigung, ich kann das Home Assistent Element {dev_name} nicht finden.
Gerät {dev_name} ist nicht auffindbar.
Entschuldigung, das Objekt {dev_name} wurde nicht gefunden.
Entschuldigung, ich konnte {dev_name} nicht finden.
Der Status von {dev_name} ist unbekannt.
4 changes: 4 additions & 0 deletions dialog/de-de/homeassistant.error.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Entschuldigung, ich kann das Home Assistent Element {dev_name} nicht finden.
Gerät {dev_name} ist nicht auffindbar.
Entschuldigung, das Objekt {dev_name} wurde nicht gefunden.
Entschuldigung, ich konnte {dev_name} nicht finden.
1 change: 1 addition & 0 deletions dialog/de-de/homeassistant.error.no_gui.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dieser Befehl kann leider nur mit einem Display verwendet werden.
1 change: 0 additions & 1 deletion dialog/de-de/homeassistant.sensor.cover.unknown.dialog

This file was deleted.

5 changes: 1 addition & 4 deletions dialog/en-us/homeassistant.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Sorry, I can't find the Home Assistant entity {dev_name}.
Entity {dev_name} could not be found.
Sorry, entity {dev_name} wasn't found.
Excuse me, I wasn't able to find {dev_name}.
The state of {dev_name} is unknown.
4 changes: 4 additions & 0 deletions dialog/en-us/homeassistant.error.device.unknown.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Sorry, I can't find the Home Assistant entity {dev_name}.
Entity {dev_name} could not be found.
Sorry, entity {dev_name} wasn't found.
Excuse me, I wasn't able to find {dev_name}.
1 change: 1 addition & 0 deletions dialog/en-us/homeassistant.error.no_gui.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sorry, but this command can only be used with a connected display.
1 change: 0 additions & 1 deletion dialog/en-us/homeassistant.sensor.cover.unknown.dialog

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions vocab/de-de/HomeAssistant.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
homeassistant
home assistant
hass
hassio
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions vocab/en-us/HomeAssistant.voc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
homeassistant
home assistant
hass
hassio
File renamed without changes.
File renamed without changes.
File renamed without changes.