54 lines
1.6 KiB
Vue
54 lines
1.6 KiB
Vue
<script setup lang="ts">
|
|
import { computed } from 'vue'
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue
|
|
} from '@/components/ui/select'
|
|
import { useSmartHomeStore, type EntitySummary } from '@/stores/smartHomeStore'
|
|
import { selectUsable } from '../../format'
|
|
import { controlFail } from './shared'
|
|
|
|
/**
|
|
* 下拉实体(`select.*`):从实体自带的 `options` 里选一个。
|
|
*
|
|
* `select.select_option` 的参数是 `option`;乐观状态直接把 state 改成所选项 ——
|
|
* 这类实体的 state 就是当前选项(真机上如"HDMI 1"、"自动")。
|
|
*
|
|
* 没给选项时不渲染(有些集成只暴露实体却不给选项表,那样点了也不知道能填什么)。
|
|
*/
|
|
const props = defineProps<{ entity: EntitySummary; disabled?: boolean }>()
|
|
const store = useSmartHomeStore()
|
|
|
|
const usable = computed(() => selectUsable(props.entity))
|
|
|
|
/** 当前选项不在 options 里时(如 HA 的 `unknown`)仍原样传给 Select,让用户自己选 */
|
|
const current = computed(() => props.entity.state)
|
|
|
|
async function pick(next: string) {
|
|
try {
|
|
await store.control(props.entity, 'select_option', { option: next }, { state: next })
|
|
} catch (e) {
|
|
controlFail(e)
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Select
|
|
v-if="usable"
|
|
:model-value="current"
|
|
:disabled="disabled"
|
|
@update:model-value="(v) => pick(String(v))"
|
|
>
|
|
<SelectTrigger class="h-8 w-full">
|
|
<SelectValue placeholder="选择…" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem v-for="o in entity.options" :key="o" :value="o">{{ o }}</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</template>
|