17 Group visibility
本系列教程主要實現通過Raspberry Pi3 + Hass.io + Macbook配合其它智能硬體實現對智能家居的控制,旨在分享搭建智能家居過程中的經驗和樂趣。
上一篇我們介紹了實體組件平台選項,今天將介紹Group visibility。通過設置Group visibility我們可以將不同的群組在界面上進行顯示和隱藏。
group.set_visibility服務可以改變組的可見性
service: group.set_visibilityentity_id: group.basementdata: visible: False
在自動化中設置組的可見性,日落隱藏,日出顯示
automation: trigger: platform: sun event: sunset action: service: group.set_visibility entity_id: group.basement data: visible: Falseautomation 2: trigger: platform: sun event: sunrise action: service: group.set_visibility entity_id: group.basement data: visible: True
1. 一個簡單的例子
在groups.yaml里創建一個天氣的組
default_view: entities: - group.weather_sensorsweather_sensors: name: Yahoo Weather entities: - sensor.yweather_temperature_min - sensor.yweather_temperature_max - sensor.yweather_wind_speed - sensor.yweather_pressure - sensor.yweather_visibility - sensor.yweather_humidity - sensor.yweather_temperature
在scripts.yaml里創建2個script,hide_yahoo_weather用於隱藏天氣組,show_yahoo_weather用於顯示天氣組。
hide_yahoo_weather: sequence: - service: group.set_visibility data_template: entity_id: group.weather_sensors visible: Falseshow_yahoo_weather: sequence: - service: group.set_visibility data_template: entity_id: group.weather_sensors visible: True
重啟以後主界面會顯示hide_yahoo_weather和show_yahoo_weather兩個script,點擊ACTIVATE分別顯示和隱藏天氣組。
2. python結合command line創建自動化設置的例子
#!/usr/bin/env python3# -*- coding: utf-8 -*-from datetime import time, datetimedef 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 bottomOCCASIONS = [ # 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_occasionif __name__ == __main__: print(get_current_occasion(OCCASIONS))
yaml如下所示,script用於測試組的可見性,automation用於設置自動化組的可見性。
group: default_view: entities: - group.work_sensors # Only visible when its time to go to work work_sensors: name: Time to go to work entities: - sensor.something1 - sensor.something2sensor: - 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
組的可見性設置就介紹到這裡,下一篇將介紹Automation。
推薦閱讀:
※用 Python 怎樣實現一個九九乘法表?
※如何學python-第十二課 邏輯運算符-成員運算符
※theano function調試有什麼好方法?
※黃哥Python,2017-9-22出題了。
TAG:智能家居 | 樹莓派RaspberryPi | Python |