2022-05-01 15:35:20 +02:00
|
|
|
<template>
|
|
|
|
<div class="entity light-container">
|
2023-01-15 14:26:44 +01:00
|
|
|
<div class="head" :class="{collapsed: collapsed}">
|
2022-05-01 15:35:20 +02:00
|
|
|
<div class="col-1 icon">
|
2023-01-15 20:02:50 +01:00
|
|
|
<EntityIcon
|
|
|
|
:entity="value"
|
|
|
|
:icon="icon"
|
|
|
|
:hasColorFill="true"
|
|
|
|
:loading="loading"
|
|
|
|
:error="error" />
|
2022-05-01 15:35:20 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="col-s-8 col-m-9 label">
|
|
|
|
<div class="name" v-text="value.name" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="col-s-3 col-m-2 buttons pull-right">
|
2022-05-30 09:23:05 +02:00
|
|
|
<ToggleSwitch :value="value.on" @input="toggle"
|
|
|
|
@click.stop :disabled="loading || value.is_read_only" />
|
|
|
|
|
2023-01-15 14:26:44 +01:00
|
|
|
<button @click.stop="collapsed = !collapsed">
|
2022-05-01 15:35:20 +02:00
|
|
|
<i class="fas"
|
2023-01-15 14:26:44 +01:00
|
|
|
:class="{'fa-angle-up': !collapsed, 'fa-angle-down': collapsed}" />
|
2022-05-01 15:35:20 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-01-15 14:26:44 +01:00
|
|
|
<div class="body" v-if="!collapsed" @click.stop="prevent">
|
2022-05-01 15:35:20 +02:00
|
|
|
<div class="row" v-if="cssColor">
|
|
|
|
<div class="icon">
|
|
|
|
<i class="fas fa-palette" />
|
|
|
|
</div>
|
|
|
|
<div class="input">
|
|
|
|
<input type="color" :value="cssColor" @change="setLight({color: $event.target.value})" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="row" v-if="value.brightness">
|
|
|
|
<div class="icon">
|
|
|
|
<i class="fas fa-sun" />
|
|
|
|
</div>
|
|
|
|
<div class="input">
|
|
|
|
<Slider :range="[value.brightness_min, value.brightness_max]"
|
|
|
|
:value="value.brightness" @input="setLight({brightness: $event.target.value})" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="row" v-if="value.saturation">
|
|
|
|
<div class="icon">
|
|
|
|
<i class="fas fa-droplet" />
|
|
|
|
</div>
|
|
|
|
<div class="input">
|
|
|
|
<Slider :range="[value.saturation_min, value.saturation_max]"
|
|
|
|
:value="value.saturation" @input="setLight({saturation: $event.target.value})" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="row" v-if="value.temperature">
|
|
|
|
<div class="icon">
|
|
|
|
<i class="fas fa-temperature-half" />
|
|
|
|
</div>
|
|
|
|
<div class="input">
|
|
|
|
<Slider :range="[value.temperature_min, value.temperature_max]"
|
|
|
|
:value="value.temperature" @input="setLight({temperature: $event.target.value})"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Slider from "@/components/elements/Slider"
|
|
|
|
import ToggleSwitch from "@/components/elements/ToggleSwitch"
|
|
|
|
import EntityMixin from "./EntityMixin"
|
|
|
|
import EntityIcon from "./EntityIcon"
|
|
|
|
import {ColorConverter} from "@/components/panels/Light/color";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Light',
|
|
|
|
components: {ToggleSwitch, Slider, EntityIcon},
|
|
|
|
mixins: [EntityMixin],
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
colorConverter: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
rgbColor() {
|
2022-05-30 09:23:05 +02:00
|
|
|
if (this.value.meta?.icon?.color)
|
|
|
|
return this.value.meta.icon.color
|
|
|
|
|
2023-01-02 12:50:01 +01:00
|
|
|
if (this.value.red && this.value.green && this.value.blue)
|
|
|
|
return ['red', 'green', 'blue'].map((c) => this.value[c])
|
|
|
|
|
|
|
|
if (!this.colorConverter)
|
|
|
|
return
|
|
|
|
|
2022-05-01 15:35:20 +02:00
|
|
|
if (
|
2023-01-02 12:50:01 +01:00
|
|
|
this.value.hue == null &&
|
|
|
|
(this.value.x == null || this.value.y == null)
|
2022-05-01 15:35:20 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if (this.value.x && this.value.y)
|
|
|
|
return this.colorConverter.xyToRgb(
|
|
|
|
this.value.x,
|
|
|
|
this.value.y,
|
|
|
|
this.value.brightness
|
|
|
|
)
|
|
|
|
|
|
|
|
return this.colorConverter.hslToRgb(
|
|
|
|
this.value.hue,
|
|
|
|
this.value.saturation,
|
|
|
|
this.value.brightness
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
cssColor() {
|
|
|
|
const rgb = this.rgbColor
|
|
|
|
if (rgb)
|
|
|
|
return this.colorConverter.rgbToHex(rgb)
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
icon() {
|
|
|
|
const icon = {...(this.value.meta?.icon || {})}
|
|
|
|
if (!icon.color && this.cssColor)
|
|
|
|
icon.color = this.cssColor
|
|
|
|
return icon
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
prevent(event) {
|
|
|
|
event.stopPropagation()
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
|
|
|
async toggle(event) {
|
|
|
|
event.stopPropagation()
|
|
|
|
this.$emit('loading', true)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.request('entities.execute', {
|
|
|
|
id: this.value.id,
|
|
|
|
action: 'toggle',
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
this.$emit('loading', false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async setLight(attrs) {
|
|
|
|
if (attrs.color) {
|
|
|
|
const rgb = this.colorConverter.hexToRgb(attrs.color)
|
|
|
|
if (this.value.x != null && this.value.y != null) {
|
|
|
|
attrs.xy = this.colorConverter.rgbToXY(...rgb)
|
|
|
|
} else if (this.value.hue != null) {
|
|
|
|
[attrs.hue, attrs.saturation, attrs.brightness] = this.colorConverter.rgbToHsl(...rgb)
|
2023-01-02 12:50:01 +01:00
|
|
|
} else if (
|
|
|
|
this.value.red != null && this.value.green != null && this.value.blue != null
|
|
|
|
) {
|
|
|
|
[attrs.red, attrs.green, attrs.blue] = [rgb.red, rgb.green, rgb.blue]
|
|
|
|
} else {
|
|
|
|
console.warn('Unrecognized color format')
|
|
|
|
console.warn(attrs.color)
|
2022-05-01 15:35:20 +02:00
|
|
|
}
|
2023-01-02 12:50:01 +01:00
|
|
|
|
|
|
|
delete attrs.color
|
2022-05-01 15:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.execute({
|
|
|
|
type: 'request',
|
|
|
|
action: this.value.plugin + '.set_lights',
|
|
|
|
args: {
|
|
|
|
lights: [this.value.external_id],
|
|
|
|
...attrs,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
const ranges = {}
|
|
|
|
if (this.value.hue)
|
|
|
|
ranges.hue = [this.value.hue_min, this.value.hue_max]
|
|
|
|
if (this.value.saturation)
|
|
|
|
ranges.sat = [this.value.saturation_min, this.value.saturation_max]
|
|
|
|
if (this.value.brightness)
|
|
|
|
ranges.bri = [this.value.brightness_min, this.value.brightness_max]
|
|
|
|
if (this.value.temperature)
|
|
|
|
ranges.ct = [this.value.temperature_min, this.value.temperature_max]
|
|
|
|
|
|
|
|
this.colorConverter = new ColorConverter(ranges)
|
|
|
|
},
|
2023-01-15 15:25:04 +01:00
|
|
|
|
|
|
|
unmounted() {
|
|
|
|
if (this.colorConverter)
|
|
|
|
delete this.colorConverter
|
|
|
|
},
|
2022-05-01 15:35:20 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
@import "common";
|
|
|
|
|
|
|
|
.light-container {
|
2022-05-30 09:23:05 +02:00
|
|
|
.head {
|
|
|
|
.buttons {
|
|
|
|
button {
|
|
|
|
margin-right: 0.5em;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 15:35:20 +02:00
|
|
|
.body {
|
|
|
|
.row {
|
|
|
|
display: flex;
|
2023-01-03 23:11:39 +01:00
|
|
|
align-items: center;
|
|
|
|
padding: 0.5em;
|
2022-05-01 15:35:20 +02:00
|
|
|
|
|
|
|
.icon {
|
|
|
|
width: 2em;
|
2023-01-03 23:11:39 +01:00
|
|
|
margin-left: -0.5em;
|
2022-05-01 15:35:20 +02:00
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.input {
|
|
|
|
width: calc(100% - 2em);
|
|
|
|
|
|
|
|
[type=color] {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
:deep(.slider) {
|
|
|
|
margin-top: 0.5em;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|