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

Feature/rgb lights #69

Open
wants to merge 16 commits into
base: 20.08
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
14 changes: 0 additions & 14 deletions .gitignore

This file was deleted.

95 changes: 75 additions & 20 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def _setup(self, force=False):
# Check if user filled IP, port and Token in configuration
if not ip:
self.speak_dialog('homeassistant.error.setup', data={
"field": "I.P."})
"field": "I.P."})
return

if not token:
self.speak_dialog('homeassistant.error.setup', data={
"field": "token"})
"field": "token"})
return

portnumber = self.settings.get('portnum')
Expand All @@ -54,7 +54,7 @@ def _setup(self, force=False):
except ValueError:
# String might be some rubbish (like '')
self.speak_dialog('homeassistant.error.setup', data={
"field": "port"})
"field": "port"})
return

self.ha = HomeAssistantClient(
Expand Down Expand Up @@ -120,7 +120,7 @@ def _check_availability(self, ha_entity):
""" Check if state is `unavailable`, if yes, inform user about it. """

self.speak_dialog('homeassistant.device.unavailable', data={
"dev_name": ha_entity['dev_name']})
"dev_name": ha_entity['dev_name']})
""" Return result to underliing function. """
return False
return True
Expand Down Expand Up @@ -151,7 +151,7 @@ def _handle_client_exception(self, callback, *args, **kwargs):
except (ConnectionError, RequestException) as exception:
# TODO find a nice member of any exception to output
self.speak_dialog('homeassistant.error', data={
'url': exception.request.url})
'url': exception.request.url})

return False

Expand All @@ -166,14 +166,16 @@ def handle_turn_on_intent(self, message):
@intent_handler('turn.off.intent')
def handle_turn_off_intent(self, message):
self.log.debug(message.data)
self.log.debug("Turn off intent on entity: "+message.data.get("entity"))
self.log.debug("Turn off intent on entity: " +
message.data.get("entity"))
message.data["Entity"] = message.data.get("entity")
message.data["Action"] = "off"
self._handle_turn_actions(message)

@intent_handler('toggle.intent')
def handle_toggle_intent(self, message):
self.log.debug("Toggle intent on entity: " + message.data.get("entity"))
self.log.debug("Toggle intent on entity: " +
message.data.get("entity"))
message.data["Entity"] = message.data.get("entity")
message.data["Action"] = "toggle"
self._handle_turn_actions(message)
Expand All @@ -186,8 +188,8 @@ def handle_sensor_intent(self, message):

@intent_handler('set.light.brightness.intent')
def handle_light_set_intent(self, message):
self.log.debug("Change light intensity: "+message.data.get("entity") \
+"to"+message.data.get("brightnessvalue")+"percent")
self.log.debug("Change light intensity: "+message.data.get("entity")
+ "to"+message.data.get("brightnessvalue")+"percent")
message.data["Entity"] = message.data.get("entity")
message.data["Brightnessvalue"] = message.data.get("brightnessvalue")
self._handle_light_set(message)
Expand All @@ -206,9 +208,54 @@ def handle_light_decrease_intent(self, message):
message.data["Action"] = "down"
self._handle_light_adjust(message)

@intent_handler('change.light.color.intent')
def handle_light_color_intent(self, message):
if not 'entity' in message.data:
self.speak_dialog('homeassistant.device.not.given',
data={"action": "change"})
return

self.log.debug("Change light colour intent on entity: " +
message.data['entity'])
self._handle_light_color(message)

def _handle_light_color(self, message):
self.log.debug("Starting change colour intent.")
self.log.debug("Entity: %s" % message.data["entity"])
self.log.debug("Color: %s" % message.data["color"])

entity = message.data['entity']
entity_parts = entity.split()
voc_match = entity

# In case the utterance is for all entities
if "all" in entity_parts:
voc_match = "all lights"

if self.voc_match(voc_match, "all_lights"):
ha_data = {'entity_id': 'all'}
ha_entity = {'dev_name': 'all lights'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So presently if you said:

Change all kitchen lights to green

That would see "all" and set the entity to "all lights" which presumably be all lights in the house?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, doesn't make sense, I removed the 'all' catch phrase

else:
ha_entity = self._find_entity(entity, ['group', 'light'])

if not ha_entity or not self._check_availability(ha_entity):
return

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love if we could find a more descriptive name for this ha_data object.

action_data?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


color = message.data['color']
color_parts = list(color.split())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does color_parts get used at all?
If not the previous line can probably be removed also.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned.


ha_data['color_name'] = message.data['color']
self.ha.execute_service("light", "turn_on", ha_data)

ha_data['dev_name'] = ha_entity['dev_name']
self.speak_dialog('homeassistant.color.change', data=ha_data)

@intent_handler('automation.intent')
def handle_automation_intent(self, message):
self.log.debug("Automation trigger intent on entity: "+message.data.get("entity"))
self.log.debug("Automation trigger intent on entity: " +
message.data.get("entity"))
message.data["Entity"] = message.data.get("entity")
self._handle_automation(message)

Expand All @@ -220,14 +267,16 @@ def handle_tracker_intent(self, message):

@intent_handler('set.climate.intent')
def handle_set_thermostat_intent(self, message):
self.log.debug("Set thermostat intent on entity: "+message.data.get("entity"))
self.log.debug("Set thermostat intent on entity: " +
message.data.get("entity"))
message.data["Entity"] = message.data.get("entity")
message.data["Temp"] = message.data.get("temp")
self._handle_set_thermostat(message)

@intent_handler('add.item.shopping.list.intent')
def handle_shopping_list_intent(self, message):
self.log.debug("Add : "+message.data.get("entity")+"to the shoping list")
self.log.debug("Add : "+message.data.get("entity") +
"to the shoping list")
message.data["Entity"] = message.data.get("entity")
self._handle_shopping_list(message)

Expand All @@ -240,9 +289,9 @@ def _handle_turn_actions(self, message):

# Handle turn on/off all intent
try:
if self.voc_match(entity,"all_lights"):
if self.voc_match(entity, "all_lights"):
domain = "light"
elif self.voc_match(entity,"all_switches"):
elif self.voc_match(entity, "all_switches"):
domain = "switch"
else:
domain = None
Expand All @@ -252,13 +301,15 @@ def _handle_turn_actions(self, message):
ha_data = {'entity_id': 'all'}

self.ha.execute_service(domain, "turn_%s" % action, ha_data)
self.speak_dialog('homeassistant.device.%s' % action, data=ha_entity)
self.speak_dialog('homeassistant.device.%s' %
action, data=ha_entity)
return
# TODO: need to figure out, if this indeed throws a KeyError
except KeyError:
self.log.debug("Not turn on/off all intent")
except:
self.log.debug("Unexpected error in turn all intent:", exc_info()[0])
self.log.debug(
"Unexpected error in turn all intent:", exc_info()[0])

# Hande single entity

Expand Down Expand Up @@ -382,14 +433,16 @@ def _handle_light_adjust(self, message):
'homeassistant.brightness.cantdim.dimmable',
data=ha_entity)
else:
ha_data['brightness'] = light_attrs['unit_measure'] - brightness_value
ha_data['brightness'] = light_attrs['unit_measure'] - \
brightness_value
if ha_data['brightness'] < min_brightness:
ha_data['brightness'] = min_brightness
self.ha.execute_service("homeassistant",
"turn_on",
ha_data)
ha_data['dev_name'] = ha_entity['dev_name']
ha_data['brightness'] = round(100 / max_brightness * ha_data['brightness'])
ha_data['brightness'] = round(
100 / max_brightness * ha_data['brightness'])
self.speak_dialog('homeassistant.brightness.decreased',
data=ha_data)
elif action == "up":
Expand All @@ -404,14 +457,16 @@ def _handle_light_adjust(self, message):
'homeassistant.brightness.cantdim.dimmable',
data=ha_entity)
else:
ha_data['brightness'] = light_attrs['unit_measure'] + brightness_value
ha_data['brightness'] = light_attrs['unit_measure'] + \
brightness_value
if ha_data['brightness'] > max_brightness:
ha_data['brightness'] = max_brightness
self.ha.execute_service("homeassistant",
"turn_on",
ha_data)
ha_data['dev_name'] = ha_entity['dev_name']
ha_data['brightness'] = round(100 / max_brightness * ha_data['brightness'])
ha_data['brightness'] = round(
100 / max_brightness * ha_data['brightness'])
self.speak_dialog('homeassistant.brightness.increased',
data=ha_data)
else:
Expand Down
2 changes: 2 additions & 0 deletions dialog/cs-cz/homeassistant.color.change.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Změněno {{dev_name}} na {{color_name}}.
{{dev_name}} změněno na {{color_name}}.
2 changes: 2 additions & 0 deletions dialog/cs-cz/homeassistant.device.not.given.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prosím, řekni mi které zařízení chceš ({{action}}|)
Které zařízení chceš {{action}}
2 changes: 2 additions & 0 deletions dialog/en-us/homeassistant.color.change.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Changed {{dev_name}} to {{color_name}}.
{{dev_name}} changed to {{color_name}}.
2 changes: 2 additions & 0 deletions dialog/en-us/homeassistant.device.not.given.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Please, tell me which device (to {{action}}|)
What device you want me to {{action}}
2 changes: 2 additions & 0 deletions vocab/cs-cz/change.light.color.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(změň|nastav) (barvu |) {{entity}} na {{color}}
(změň|nastav) {{entity}} na {{color}} (barvu|)
2 changes: 2 additions & 0 deletions vocab/en-us/change.light.color.intent
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
change (color (of|) |) {{entity}} to {{color}}
change {{entity}} (color|) to {{color}}