『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 5479|回复: 3

Group组的隐藏和显示

[复制链接]

175

主题

2956

帖子

7555

积分

超级版主

我就是六神

Rank: 8Rank: 8

积分
7555
金钱
4574
HASS币
398

活跃会员教程狂人灌水之王

QQ
发表于 2018-2-24 11:12:28 | 显示全部楼层 |阅读模式
本帖最后由 jyz_0501 于 2018-2-24 12:49 编辑

官方文档:https://home-assistant.io/docs/c ... sibility-of-a-group

在向homeassistant填写所有的自动化设备之后,通常最终会出现一个混乱的界面和许多在当前环境中不感兴趣的组。 如果您只想展示现在有用的groups并隐藏其余groups,该怎么办? 这时候就需要 group
visibility 参数。

  • 改变组的可见性
要更改组的可见性,请使用服务group.set_visibility,将组名称作为entity_id传递,并使用visible来决定是显示还是隐藏组。
示例:
service: group.set_visibility
entity_id: group.basement
data:
  visible: False

提示:如果传感器只属于一个组,并且该组隐藏,则传感器将“跳转”到Web界面的顶部。 如果您不希望发生这种情况,请将传感器添加到其他(可见)组。

  • 自动化
首先,您应该决定在哪些情况下应该显示或不显示群组。 根据复杂性,您可能必须编写两个自动化程序:一个隐藏组和另一个显示它。

在这个例子中,group.basement在太阳落下时会隐藏,当它升起时会再次显示:
automation:
  trigger:
    platform: sun
    event: sunset
  action:
    service: group.set_visibility
    entity_id: group.basement
    data:
      visible: False

automation 2:
  trigger:
    platform: sun
    event: sunrise
  action:
    service: group.set_visibility
    entity_id: group.basement
    data:
      visible: True


  • 简易自动化
最常见的用例之一是在一天中的某些时段显示群组,可能在工作日早晨通勤信息或天黑时开灯。 实现这一点所需的自动化的复杂性将让你束手无措。 因此,简化自动化的一种方法是创建一个根据一天中的时间改变其状态的传感器。 一种做法是使用一个command_line传感器和一个脚本:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from datetime import time, datetime

def mk_occasion(name, start, end, days=None):
    s = start.split(':')
    e = end.split(':')
    return {'name' : name,
            'start': time(int(s[0]), int(s[1]), int(s[2])),
            'end'  : time(int(e[0]), int(e[1]), int(e[2])),
            'days' : days}

# Matching is done from top to bottom
OCCASIONS = [
    # More specific occasions
    mk_occasion('work_morning', '06:00:00', '07:10:00', range(5)),

    # General matching
    mk_occasion('weekday', '00:00:00', '23:59:59', range(5)),
    mk_occasion('weekend', '00:00:00', '23:59:59', [5, 6])
]

def get_current_occasion(occasion_list, default_occasion='normal'):
    now = datetime.now()
    for occasion in OCCASIONS:
        if occasion['start'] <= now.time() <= occasion['end'] and \
           (occasion['days'] is None or now.weekday() in occasion['days']):
            return occasion['name']
    return default_occasion

if __name__ == '__main__':
    print(get_current_occasion(OCCASIONS))


该脚本将在星期一至星期五的06:00-07:10以及星期一至星期五的所有其他时间和星期六及星期日的“周末”输出“work_morning”。 根据您的需求进行调整。 要创建传感器,只需像这样添加它:
sensor:
  - platform: command_line
    name: Occasion
    command: "python3 occasion.py"

提示:如果您使用docker运行Home Assistant,那么occasion.py脚本将放置在/ config下。 你的命令应该是:command:“python3 /command/occasion.py”
script:
  group_visibility:
    sequence:
      - service: group.set_visibility
        data_template:
          entity_id: '{{ entity_id }}'
          visible: '{{ is_state(cond, visible_state) }}'


最后一部分是编写隐藏或显示组的自动化:
automation:
  - alias: Work morning
    trigger:
      - platform: state
        entity_id: sensor.occasion
      - platform: homeassistant
        event: start
    action:
      service: script.group_visibility
      data:
        entity_id: group.work_sensors
        cond: sensor.occasion
        visible_state: 'work_morning'

如果sensor.occasion更改状态或Home Assistant启动时,我们之前定义的脚本将被调用。 当sensor.occasion将状态更改为“work_morning”并以其他方式隐藏时,组group.work_sensors将显示。

  • 组件示例
group:
  default_view:
    entities:
      - group.work_sensors

  # Only visible when it's time to go to work
  work_sensors:
    name: Time to go to work
    entities:
      - sensor.something1
      - sensor.something2

sensor:
  - platform: command_line
    name: Occasion
    command: "python3 occasion.py"

script:
  group_visibility:
    sequence:
      - service: group.set_visibility
        data_template:
          entity_id: '{{ entity_id }}'
          visible: '{{ is_state(cond, visible_state) }}'

automation:
  - alias: Work morning
    trigger:
      - platform: state
        entity_id: sensor.occasion
      - platform: homeassistant
        event: start
    action:
      service: script.group_visibility
      data:
        entity_id: group.work_sensors
        cond: sensor.occasion
        visible_state: 'work_morning'


评分

参与人数 2金钱 +20 收起 理由
27hh + 10 分享是一种美德!
+ 10 分享是一种美德!

查看全部评分

回复

使用道具 举报

0

主题

316

帖子

1732

积分

金牌会员

Rank: 6Rank: 6

积分
1732
金钱
1416
HASS币
0
发表于 2018-2-24 11:29:42 | 显示全部楼层
给大大点赞,就是有时候就是这样的小细节,我这样的小白找半天
回复

使用道具 举报

123

主题

4630

帖子

1万

积分

管理员

囧死

Rank: 9Rank: 9Rank: 9

积分
16087
金钱
11372
HASS币
45
发表于 2018-2-24 11:59:54 | 显示全部楼层
首先感谢楼主分享,如果能摘录过来,并加上中文描述就更好了!
回复

使用道具 举报

175

主题

2956

帖子

7555

积分

超级版主

我就是六神

Rank: 8Rank: 8

积分
7555
金钱
4574
HASS币
398

活跃会员教程狂人灌水之王

QQ
 楼主| 发表于 2018-2-24 12:49:41 | 显示全部楼层
Jones 发表于 2018-2-24 11:59
首先感谢楼主分享,如果能摘录过来,并加上中文描述就更好了!

应邀添加了~~~哈哈~~~

评分

参与人数 1金钱 +12 收起 理由
+ 12 棒棒的!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-17 11:02 , Processed in 1.500867 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表