diff --git a/__init__.py b/__init__.py index 30df3175..82fdde25 100644 --- a/__init__.py +++ b/__init__.py @@ -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') @@ -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( @@ -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 @@ -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 @@ -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) @@ -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) @@ -206,9 +208,46 @@ 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'] + voc_match = entity + + if self.voc_match(voc_match, "all_lights"): + action_data = {'entity_id': 'all'} + ha_entity = {'dev_name': 'all lights'} + else: + ha_entity = self._find_entity(entity, ['group', 'light']) + + if not ha_entity or not self._check_availability(ha_entity): + return + + action_data = {'entity_id': ha_entity['id']} + + action_data['color_name'] = message.data['color'] + self.ha.execute_service("light", "turn_on", action_data) + + action_data['dev_name'] = ha_entity['dev_name'] + self.speak_dialog('homeassistant.color.change', data=action_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) @@ -220,14 +259,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) @@ -240,9 +281,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 @@ -252,13 +293,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 @@ -382,14 +425,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": @@ -404,14 +449,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: diff --git a/dialog/cs-cz/homeassistant.color.change.dialog b/dialog/cs-cz/homeassistant.color.change.dialog new file mode 100644 index 00000000..31ef1b7f --- /dev/null +++ b/dialog/cs-cz/homeassistant.color.change.dialog @@ -0,0 +1,2 @@ +Změněno {{dev_name}} na {{color_name}}. +{{dev_name}} změněno na {{color_name}}. diff --git a/dialog/cs-cz/homeassistant.device.not.given.dialog b/dialog/cs-cz/homeassistant.device.not.given.dialog new file mode 100644 index 00000000..c4e68d86 --- /dev/null +++ b/dialog/cs-cz/homeassistant.device.not.given.dialog @@ -0,0 +1,2 @@ +Prosím, řekni mi které zařízení chceš ({{action}}|) +Které zařízení chceš {{action}} diff --git a/dialog/en-us/homeassistant.color.change.dialog b/dialog/en-us/homeassistant.color.change.dialog new file mode 100644 index 00000000..e8626ff0 --- /dev/null +++ b/dialog/en-us/homeassistant.color.change.dialog @@ -0,0 +1,2 @@ +Changed {{dev_name}} to {{color_name}}. +{{dev_name}} changed to {{color_name}}. diff --git a/dialog/en-us/homeassistant.device.not.given.dialog b/dialog/en-us/homeassistant.device.not.given.dialog new file mode 100644 index 00000000..62e7d15a --- /dev/null +++ b/dialog/en-us/homeassistant.device.not.given.dialog @@ -0,0 +1,2 @@ +Please, tell me which device (to {{action}}|) +What device you want me to {{action}} diff --git a/vocab/cs-cz/change.light.color.intent b/vocab/cs-cz/change.light.color.intent new file mode 100644 index 00000000..4d132e1e --- /dev/null +++ b/vocab/cs-cz/change.light.color.intent @@ -0,0 +1,2 @@ +(změň|nastav) (barvu |) {{entity}} na {{color}} +(změň|nastav) {{entity}} na {{color}} (barvu|) diff --git a/vocab/en-us/change.light.color.intent b/vocab/en-us/change.light.color.intent new file mode 100644 index 00000000..1cce80f5 --- /dev/null +++ b/vocab/en-us/change.light.color.intent @@ -0,0 +1,2 @@ +change (color (of|) |) {{entity}} to {{color}} +change {{entity}} (color|) to {{color}}