def patch(self, state): if "state" in state: self.state = state["state"] == "ON" if "brightness" in state: brightness = rescale(state["brightness"], 100, 255) self.brightness = brightness if "hue" in state: self.hue = state["hue"] self.bulb_mode = BULB_MODE_COLOR if "saturation" in state: self.saturation = state["saturation"] self.bulb_mode = BULB_MODE_COLOR if "mode" in state: self.mode = state["mode"] self.bulb_mode = BULB_MODE_SCENE if "color_temp" in state: self.mireds = state["color_temp"] self.bulb_mode = BULB_MODE_WHITE # Any changes other than setting mode to night should take device out of # night mode. self.night_mode = False if "command" in state: command = state["command"] if command == "white_mode": self.bulb_mode = BULB_MODE_WHITE elif command == "night_mode": self.bulb_mode = BULB_MODE_NIGHT
def apply_field(self, partial_state, field): if self.state is not None and field == "state" or field == "status": if self.state: state = "ON" else: state = "OFF" partial_state["state"] = state return if self.brightness is not None and field == "brightness": partial_state["brightness"] = rescale(self.brightness, 255, 100) return if self.brightness is not None and field == "level": partial_state["level"] = self.brightness return if self.bulb_mode is not None and field == "bulb_mode": partial_state["bulb_mode"] = BULB_MODE_NAMES[self.bulb_mode] return if field == "color" and self.bulb_mode == BULB_MODE_COLOR: self.apply_color(partial_state) return if field == "computed_color": if self.bulb_mode == BULB_MODE_COLOR: self.apply_color(partial_state) elif self.bulb_mode is not None: self.apply_rgb_color(partial_state, 255, 255, 255) return if self.hue is not None and field == "hue" and self.bulb_mode == BULB_MODE_COLOR: partial_state["hue"] = self.hue return if self.saturation is not None and field == "saturation" and self.bulb_mode == BULB_MODE_COLOR: partial_state["saturation"] = self.saturation return if self.mode is not None and field == "mode" and self.bulb_mode == BULB_MODE_SCENE: partial_state["mode"] = self.mode return if field == "effect": if self.bulb_mode == BULB_MODE_SCENE: partial_state["effect"] = str(self.mode) elif self.bulb_mode == BULB_MODE_WHITE: partial_state["effect"] = "white_mode" elif self.bulb_mode == BULB_MODE_NIGHT: partial_state["effect"] = "night_mode" return if self.mireds is not None and field == "color_temp" and self.bulb_mode == BULB_MODE_WHITE: partial_state["color_temp"] = self.mireds return if self.kelvin is not None and field == "kelvin" and self.bulb_mode == BULB_MODE_WHITE: partial_state["kelvin"] = self.kelvin
def hue(self): if self._hue is None: return None return rescale(self._hue, 360, 255)
def hue(self, value): self.set_dirty() self._hue = rescale(value, 255, 360)
def request_to_packets(self, request): formatter = self.radios[self.current_radio].formatter parsed_status = self.parse_status(request) # Always turn on first if parsed_status == ON: yield formatter.update_status(ON) commands = [] if "command" in request: commands = [request["command"]] if "commands" in request: commands = request["commands"] for command in commands: yield self.handle_command(command) # /omeassistant - Handle effect if "effect" in request: yield self.handle_effect(request["effect"]) if "hue" in request: yield formatter.update_hue(request["hue"]) if "saturation" in request: yield formatter.update_saturation(request["saturation"]) # Convert RGB to HSV if "color" in request: color = request["color"] red = color["r"] green = color["g"] blue = color["b"] # If close to white if red > 256 - self.RGB_WHITE_BOUNDARY and \ green > 256 - self.RGB_WHITE_BOUNDARY and \ blue > 256 - self.RGB_WHITE_BOUNDARY: yield formatter.update_color_white() else: hsv = rgb_to_hsv(red, green, blue) hue = int(round(hsv[0] * 360, 0)) saturation = int(round(hsv[1] * 100, 0)) yield formatter.update_hue(hue) yield formatter.update_saturation(saturation) if "level" in request: yield formatter.update_brightness(request["level"]) # HomeAssistant if "brightness" in request: scaled_brightness = rescale(int(request["brightness"]), 100, 255) yield formatter.update_brightness(scaled_brightness) if "temperature" in request: yield formatter.update_temperature(request["temperature"]) # HomeAssistant if "color_temp" in request: yield formatter.update_temperature( mireds_to_white_val(request["color_temp"])) if "mode" in request: yield formatter.update_mode(request["mode"]) # Raw packet command/args if "button_id" in request and "argument" in request: yield formatter.command(request["button_id"], request["argument"]) # Always turn off last if parsed_status == OFF: yield formatter.update_status(OFF)