async def async_set_cover_position(self, **kwargs): """Move the cover to a specific position.""" set_position_template = self._config.get(CONF_SET_POSITION_TEMPLATE) if ATTR_POSITION in kwargs: position = kwargs[ATTR_POSITION] percentage_position = position if set_position_template is not None: try: position = set_position_template.async_render(**kwargs) except TemplateError as ex: _LOGGER.error(ex) self._state = None elif (self._config[CONF_POSITION_OPEN] != 100 and self._config[CONF_POSITION_CLOSED] != 0): position = self.find_in_range_from_percent( position, COVER_PAYLOAD) mqtt.async_publish( self.opp, self._config.get(CONF_SET_POSITION_TOPIC), position, self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._optimistic: self._state = (STATE_CLOSED if percentage_position == self._config[CONF_POSITION_CLOSED] else STATE_OPEN) self._position = percentage_position self.async_write_op_state()
async def async_set_speed(self, speed: str) -> None: """Set the speed of the fan. This method is a coroutine. """ if self._topic[CONF_SPEED_COMMAND_TOPIC] is None: return if speed == SPEED_LOW: mqtt_payload = self._payload["SPEED_LOW"] elif speed == SPEED_MEDIUM: mqtt_payload = self._payload["SPEED_MEDIUM"] elif speed == SPEED_HIGH: mqtt_payload = self._payload["SPEED_HIGH"] elif speed == SPEED_OFF: mqtt_payload = self._payload["SPEED_OFF"] else: mqtt_payload = speed mqtt.async_publish( self.opp, self._topic[CONF_SPEED_COMMAND_TOPIC], mqtt_payload, self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._optimistic_speed: self._speed = speed self.async_write_op_state()
async def _async_state_changed_listener(self, event): """Publish state change to MQTT.""" new_state = event.data.get("new_state") if new_state is None: return mqtt.async_publish(self.opp, self._state_topic, new_state.state, self._qos, True)
async def message_received(msg): """Handle new messages on MQTT.""" _LOGGER.debug("New intent: %s", msg.payload) try: request = json.loads(msg.payload) except TypeError: _LOGGER.error("Received invalid JSON: %s", msg.payload) return if request["intent"]["confidenceScore"] < config[DOMAIN].get( CONF_PROBABILITY): _LOGGER.warning( "Intent below probaility threshold %s < %s", request["intent"]["confidenceScore"], config[DOMAIN].get(CONF_PROBABILITY), ) return try: request = INTENT_SCHEMA(request) except vol.Invalid as err: _LOGGER.error("Intent has invalid schema: %s. %s", err, request) return if request["intent"]["intentName"].startswith("user_"): intent_type = request["intent"]["intentName"].split("__")[-1] else: intent_type = request["intent"]["intentName"].split(":")[-1] slots = {} for slot in request.get("slots", []): slots[slot["slotName"]] = {"value": resolve_slot_values(slot)} slots["{}_raw".format(slot["slotName"])] = { "value": slot["rawValue"] } slots["site_id"] = {"value": request.get("siteId")} slots["session_id"] = {"value": request.get("sessionId")} slots["confidenceScore"] = { "value": request["intent"]["confidenceScore"] } try: intent_response = await intent.async_handle( opp, DOMAIN, intent_type, slots, request["input"]) notification = {"sessionId": request.get("sessionId", "default")} if "plain" in intent_response.speech: notification["text"] = intent_response.speech["plain"][ "speech"] _LOGGER.debug("send_response %s", json.dumps(notification)) mqtt.async_publish(opp, "hermes/dialogueManager/endSession", json.dumps(notification)) except intent.UnknownIntent: _LOGGER.warning("Received unknown intent %s", request["intent"]["intentName"]) except intent.IntentError: _LOGGER.exception("Error while handling intent: %s", intent_type)
def _publish(self, topic, payload): if self._topic[topic] is not None: mqtt.async_publish( self.opp, self._topic[topic], payload, self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_set_fan_speed(self, fan_speed, **kwargs): """Set fan speed.""" if (self.supported_features & SUPPORT_FAN_SPEED == 0) or fan_speed not in self._fan_speed_list: return None mqtt.async_publish(self.opp, self._set_fan_speed_topic, fan_speed, self._qos, self._retain) self._status = f"Setting fan to {fan_speed}..." self.async_write_op_state()
async def async_return_to_base(self, **kwargs): """Tell the vacuum to return to its dock.""" if self.supported_features & SUPPORT_RETURN_HOME == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_RETURN_TO_BASE], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_locate(self, **kwargs): """Locate the vacuum (usually by playing a song).""" if self.supported_features & SUPPORT_LOCATE == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_LOCATE], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_clean_spot(self, **kwargs): """Perform a spot clean-up.""" if self.supported_features & SUPPORT_CLEAN_SPOT == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_CLEAN_SPOT], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_start(self): """Start the vacuum.""" if self.supported_features & SUPPORT_START == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_START], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_stop(self, **kwargs): """Stop the vacuum.""" if self.supported_features & SUPPORT_STOP == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_STOP], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_pause(self): """Pause the vacuum.""" if self.supported_features & SUPPORT_PAUSE == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._config[CONF_PAYLOAD_PAUSE], self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_stop_cover(self, **kwargs): """Stop the device. This method is a coroutine. """ mqtt.async_publish( self.opp, self._config.get(CONF_COMMAND_TOPIC), self._config[CONF_PAYLOAD_STOP], self._config[CONF_QOS], self._config[CONF_RETAIN], )
def _publish(self, code, action): """Publish via mqtt.""" command_template = self._config[CONF_COMMAND_TEMPLATE] values = {"action": action, "code": code} payload = command_template.async_render(**values) mqtt.async_publish( self.opp, self._config[CONF_COMMAND_TOPIC], payload, self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def async_set_fan_speed(self, fan_speed, **kwargs): """Set fan speed.""" if (self.supported_features & SUPPORT_FAN_SPEED == 0) or (fan_speed not in self._fan_speed_list): return None mqtt.async_publish( self.opp, self._set_fan_speed_topic, fan_speed, self._config[CONF_QOS], self._config[CONF_RETAIN], )
async def snips_say(call): """Send a Snips notification message.""" notification = { "siteId": call.data.get(ATTR_SITE_ID, "default"), "customData": call.data.get(ATTR_CUSTOM_DATA, ""), "init": { "type": "notification", "text": call.data.get(ATTR_TEXT) }, } mqtt.async_publish(opp, "hermes/dialogueManager/startSession", json.dumps(notification)) return
async def async_close_cover_tilt(self, **kwargs): """Tilt the cover closed.""" mqtt.async_publish( self.opp, self._config.get(CONF_TILT_COMMAND_TOPIC), self._config[CONF_TILT_CLOSED_POSITION], self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._tilt_optimistic: self._tilt_value = self.find_percentage_in_range( float(self._config[CONF_TILT_CLOSED_POSITION])) self.async_write_op_state()
async def async_send_command(self, command, params=None, **kwargs): """Send a command to a vacuum cleaner.""" if self.supported_features & SUPPORT_SEND_COMMAND == 0: return if params: message = {"command": command} message.update(params) message = json.dumps(message) else: message = command mqtt.async_publish(self.opp, self._send_command_topic, message, self._qos, self._retain) self._status = f"Sending command {message}..." self.async_write_op_state()
async def async_return_to_base(self, **kwargs): """Tell the vacuum to return to its dock.""" if self.supported_features & SUPPORT_RETURN_HOME == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_RETURN_TO_BASE], self._qos, self._retain, ) self._status = "Returning home..." self.async_write_op_state()
async def async_locate(self, **kwargs): """Locate the vacuum (usually by playing a song).""" if self.supported_features & SUPPORT_LOCATE == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_LOCATE], self._qos, self._retain, ) self._status = "Hi, I'm over here!" self.async_write_op_state()
async def async_start_pause(self, **kwargs): """Start, pause or resume the cleaning task.""" if self.supported_features & SUPPORT_PAUSE == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_START_PAUSE], self._qos, self._retain, ) self._status = "Pausing/Resuming cleaning..." self.async_write_op_state()
async def async_stop(self, **kwargs): """Stop the vacuum.""" if self.supported_features & SUPPORT_STOP == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_STOP], self._qos, self._retain, ) self._status = "Stopping the current task" self.async_write_op_state()
async def async_clean_spot(self, **kwargs): """Perform a spot clean-up.""" if self.supported_features & SUPPORT_CLEAN_SPOT == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_CLEAN_SPOT], self._qos, self._retain, ) self._status = "Cleaning spot" self.async_write_op_state()
async def async_turn_off(self, **kwargs): """Turn the vacuum off.""" if self.supported_features & SUPPORT_TURN_OFF == 0: return None mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_TURN_OFF], self._qos, self._retain, ) self._status = "Turning Off" self.async_write_op_state()
async def async_turn_on(self, **kwargs): """Turn the vacuum on.""" if self.supported_features & SUPPORT_TURN_ON == 0: return mqtt.async_publish( self.opp, self._command_topic, self._payloads[CONF_PAYLOAD_TURN_ON], self._qos, self._retain, ) self._status = "Cleaning" self.async_write_op_state()
async def snips_say_action(call): """Send a Snips action message.""" notification = { "siteId": call.data.get(ATTR_SITE_ID, "default"), "customData": call.data.get(ATTR_CUSTOM_DATA, ""), "init": { "type": "action", "text": call.data.get(ATTR_TEXT), "canBeEnqueued": call.data.get(ATTR_CAN_BE_ENQUEUED, True), "intentFilter": call.data.get(ATTR_INTENT_FILTER, []), }, } mqtt.async_publish(opp, "hermes/dialogueManager/startSession", json.dumps(notification)) return
async def async_turn_off(self, **kwargs) -> None: """Turn off the entity. This method is a coroutine. """ mqtt.async_publish( self.opp, self._topic[CONF_COMMAND_TOPIC], self._payload["STATE_OFF"], self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._optimistic: self._state = False self.async_write_op_state()
async def async_unlock(self, **kwargs): """Unlock the device. This method is a coroutine. """ mqtt.async_publish( self.opp, self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_UNLOCK], self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._optimistic: # Optimistically assume that the lock has changed state. self._state = False self.async_write_op_state()
async def async_turn_off(self, **kwargs): """Turn the device off. This method is a coroutine. """ mqtt.async_publish( self.opp, self._config[CONF_COMMAND_TOPIC], self._config[CONF_PAYLOAD_OFF], self._config[CONF_QOS], self._config[CONF_RETAIN], ) if self._optimistic: # Optimistically assume that switch has changed state. self._state = False self.async_write_op_state()
async def async_turn_on(self, speed: str = None, **kwargs) -> None: """Turn on the entity. This method is a coroutine. """ mqtt.async_publish( self.opp, self._topic[CONF_COMMAND_TOPIC], self._payload["STATE_ON"], self._config[CONF_QOS], self._config[CONF_RETAIN], ) if speed: await self.async_set_speed(speed) if self._optimistic: self._state = True self.async_write_op_state()