我已经开发基于家庭助手的智能家居设置大约一年了 (自从离开SmartThings) 在经历了最初的陡峭学习曲线之后,我一直对家庭助理感到非常满意,因此我开始扩大它的功能,因为我确信它为我的智能家居提供了长期基础. 我想添加的一件事是垃圾箱收集数据,以提醒我什么时候该垃圾箱到期. 当我住在谢菲尔德时,我需要从地方当局外包提供商处获取此数据 (威立雅). 以下详细说明了我如何做到这一点, 在其他地区已经取得成功的其他人的工作基础上
2021 年 12 月 20 日更新
I’ve made a change to both ths bash script AND the template — the first to make sure required packages are available and the second to fix a breaking change made by recent releases of home assistant
我使用了一个脚本,该脚本是从各种示例中修改的 RobBrad 的 HA 支持线程. 为了运行这个脚本,我需要在 home-assistant 上安装 beautifulsoup 模块.
添加脚本
- 我使用“Samba Share”主管插件直接从我的 Windows PC 访问我的家庭助理文件——除非你已经有了首选的访问方法,我建议你也这样做
- 浏览到 \\ha-ip-address config
- 创建一个 python脚本 夹
- 在这个文件夹中创建一个新的 python 脚本——我叫我的 bin_collection.py
- 插入以下脚本1234567891011121314151617181920212223242526272829303132333435import sysimport subprocessimport pkg_resourcesrequired = {‘beautifulsoup4’, ‘python-dateutil’}installed = {pkg.key for pkg in pkg_resources.working_set}missing = required - installedif missing:python = sys.executablesubprocess.check_call([python3, ‘-m’, ‘pip3’, ‘install’, *missing], stdout=subprocess.DEVNULL)from bs4 import BeautifulSoupimport datetimefrom dateutil import parserimport requestsimport jsonurl = ‘https://wasteservices.sheffield.gov.uk/property/############’page = requests.get(url)if page.status_code != 200:exit(1)soup = BeautifulSoup(page.text, ‘html.parser’)out = {}bh3s = soup.find_all(‘td’, class_=“service-name”)bpds = soup.find_all(‘td’, class_=“next-service”)for i in range(len(bpds)):bin_colour = str(bh3s[i].contents[3]).lower().split(‘>’)[1].split(‘ ’)[0]out[bin_colour] = parser.parse(bpds[i].contents[2].lstrip().split(‘,’)[0]).strftime(‘%Y-%m-%d’)print(json.dumps(out))
- 您将需要更换 ############ 带有您财产的唯一编号,我将解释如何在下一步中获得
获取您的唯一地址
- 浏览 https://wasteservices.sheffield.gov.uk/property/
- 输入您的邮政编码,然后从列表中选择您的地址
- 记下新网址. 在上面脚本的 url 行中使用它 - 您需要更改的唯一部分是 12 最后的数字
从configuration.yaml调用脚本
- 使脚本运行所需的只是configuration.yaml中的一个简单条目
- 您可能需要调整运行频率. 矿山每天运行一次.123456sensor:- platform: command_linename: “Bin collections”command: “python3 /config/python-scripts/bin_collection.py”scan_interval: 86400command_timeout: 60 #needed as the website is slow to respond and will often timeout if left at default
使用configuration.yaml为每种类型的bin创建实体
- 紧接上述部分 (所以仍然在传感器下: 仲tion) 添加如下1234567891011121314- platform: templatesensors:black_bin:device_class: timestampvalue_template: ‘{{ strptime((states(“sensor.bin_collections”)|from_json())[“black”], “%Y-%m-%d”) | as_local }}’unique_id: “black_bin”brown_bin:device_class: timestampvalue_template: ‘{{ strptime((states(“sensor.bin_collections”)|from_json())[“brown”], “%Y-%m-%d”) | as_local }}’unique_id: “brown_bin”blue_bin:device_class: timestampvalue_template: ‘{{ strptime((states(“sensor.bin_collections”)|from_json())[“blue”], “%Y-%m-%d”) | as_local }}’unique_id: “blue_bin”
在前端显示结果
- 要在前端显示结果,只需添加一张带有 3 传感器就可以了 (特别是sensor.black_bin, sensor.blue_bin, 和sensor.brown_bin)
- 请注意,您可能需要重新启动家庭助理才能加载您添加到 configuration.yaml 的新传感器
有你自己的一些想法? 下面放纵自己通过评论! 如果您想订阅,请使用菜单上的订阅链接右上方. 您还可以通过使用下面的链接社会分享这与你的朋友. 干杯.
Leave a Reply