├── custom_components └── oilprice │ ├── __init__.py │ ├── __pycache__ │ ├── sensor.cpython-39.pyc │ └── __init__.cpython-39.pyc │ ├── manifest.json │ └── sensor.py ├── 油价卡片代码 (横排).txt ├── 油价卡片代码 (竖排).txt ├── LICENSE └── README.md /custom_components/oilprice/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /custom_components/oilprice/__pycache__/sensor.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheny95/olipirce/HEAD/custom_components/oilprice/__pycache__/sensor.cpython-39.pyc -------------------------------------------------------------------------------- /custom_components/oilprice/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheny95/olipirce/HEAD/custom_components/oilprice/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /custom_components/oilprice/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "domain": "oilprice", 3 | "name": "oilprice", 4 | "version": "0.0.2", 5 | "documentation": "https://github.com/aalavender/OilPrice/blob/master/README.md", 6 | "dependencies": [], 7 | "codeowners": ["@aalavender"], 8 | "requirements": ["requests", "beautifulsoup4"] 9 | } 10 | -------------------------------------------------------------------------------- /油价卡片代码 (横排).txt: -------------------------------------------------------------------------------- 1 | {{ state_attr('sensor.zui_xin_you_jie', 'update_time')}} 2 | ##
92# {{ state_attr('sensor.zui_xin_you_jie', '92')}}                    95# {{ state_attr('sensor.zui_xin_you_jie', '95')}}

98# {{ state_attr('sensor.zui_xin_you_jie', '98')}}              0#柴油 {{ state_attr('sensor.zui_xin_you_jie', '0')}}

3 | - {{ states('sensor.zui_xin_you_jie') }} 4 | - {{ state_attr('sensor.zui_xin_you_jie', 'tips')}} 5 | -------------------------------------------------------------------------------- /油价卡片代码 (竖排).txt: -------------------------------------------------------------------------------- 1 | ## 2 | ##
海安油价 3 | {{ state_attr('sensor.zui_xin_you_jie', 'update_time')}} 4 | ##
92# {{ state_attr('sensor.zui_xin_you_jie', '92')}}                 5 | ##
95# {{ state_attr('sensor.zui_xin_you_jie', '95')}}                 6 | ##
98# {{ state_attr('sensor.zui_xin_you_jie', '98')}}                 7 | ##
0# {{ state_attr('sensor.zui_xin_you_jie', '0')}}                8 | - {{ states('sensor.zui_xin_you_jie') }} 9 | - {{ state_attr('sensor.zui_xin_you_jie', 'tips')}} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Chan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 油价 HomeAssistant集成 2 | 3 | ![yaofan](https://user-images.githubusercontent.com/6293952/196037499-17ef6aec-9fe4-4fc2-a4ac-811a12bfb380.jpg) 4 | 5 | 6 | ## 安装/更新 7 | 8 | > 以下两种方法任选其一! 9 | 10 | #### 方法1: [HACS (**点击这里安装**)](https://my.home-assistant.io/redirect/hacs_repository/?owner=cheny95&repository=olipirce&category=integration) 11 | 12 | #### 方法2: 通过 Samba / SFTP 手动安装 13 | > [下载](https://github.com/cheny95/olipirce/archive/main.zip)解压并复制`custom_components/oilprice`文件夹到HA配置目录下的`custom_components`文件夹 14 | 15 | ## 使用方法: 16 | - 安装完毕后重启Home Assistant。 17 | - 启动完成后,在`/config/configuration.yaml`添加以下内容 18 | 19 | ```yaml 20 | sensor: 21 | - platform: oilprice 22 | name: 油价 23 | region: anhui # 此处为你要获取油价的省份全拼 24 | ``` 25 | - 再次重启 Home Assistant 26 | - 启动成功后,如无报错,你的油价插件就已安装完成 27 | 28 | ## 查看是否成功: 29 | 30 | - 开发者工具-状态-实体-输入筛选实体 31 | - 输入刚才你定义的名字『油价』 32 | - 查看是否有数据,形如: 33 | image 34 | 35 | 36 | ## 面板调用: 37 | 38 | - 打开概览页面-右上角三个点-编辑仪表盘 39 | - 右下角添加卡片-搜索「markdown」,添加markdown卡片 40 | image 41 | 42 | - 标题按照喜好输入,内容直接拷贝仓库中提供的横排或竖排的代码即可。 43 | image 44 | 45 | - 保存,完成编辑,查看效果。 46 | 47 | ## 交流 48 | - QQ群:198841186 49 | 50 | - 微信群:(添加该机器人,发送“进群”会自动发送邀请链接) 51 | 52 | ![xiaomi miot weixin group](https://user-images.githubusercontent.com/4549099/161735971-0540ce1c-eb49-4aff-8cb3-3bdad15e22f7.png) 53 | -------------------------------------------------------------------------------- /custom_components/oilprice/sensor.py: -------------------------------------------------------------------------------- 1 | """ 2 | A component which allows you to parse http://www.qiyoujiage.com/zhejiang.shtml get oil price 3 | 4 | For more details about this component, please refer to the documentation at 5 | https://github.com/aalavender/OilPrice/ 6 | 7 | """ 8 | import re 9 | import logging 10 | import asyncio 11 | import voluptuous as vol 12 | import datetime 13 | from homeassistant.helpers.entity import Entity 14 | import homeassistant.helpers.config_validation as cv 15 | from homeassistant.components.sensor import (PLATFORM_SCHEMA) 16 | from homeassistant.const import (CONF_NAME, CONF_REGION) 17 | from requests import request 18 | from bs4 import BeautifulSoup 19 | 20 | __version__ = '0.1.0' 21 | _LOGGER = logging.getLogger(__name__) 22 | 23 | REQUIREMENTS = ['requests', 'beautifulsoup4'] 24 | 25 | COMPONENT_REPO = 'https://github.com/aalavender/OilPrice/' 26 | SCAN_INTERVAL = datetime.timedelta(hours=8) 27 | ICON = 'mdi:gas-station' 28 | 29 | PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ 30 | vol.Required(CONF_NAME): cv.string, 31 | vol.Required(CONF_REGION): cv.string, 32 | }) 33 | 34 | async def async_setup_platform(hass, config, async_add_devices, discovery_info=None): 35 | _LOGGER.info("async_setup_platform sensor oilprice") 36 | async_add_devices([OilPriceSensor(name=config[CONF_NAME], region=config[CONF_REGION])],True) 37 | 38 | # @asyncio.coroutine 39 | # def async_setup_platform(hass, config, async_add_devices, discovery_info=None): 40 | # _LOGGER.info("async_setup_platform sensor oilprice") 41 | # async_add_devices([OilPriceSensor(name=config[CONF_NAME], region=config[CONF_REGION])],True) 42 | 43 | 44 | class OilPriceSensor(Entity): 45 | def __init__(self, name: str, region: str): 46 | self._name = name 47 | self._region = region 48 | self._state = None 49 | self._entries = {} 50 | 51 | def update(self): 52 | _LOGGER.info("sensor oilprice update info from http://www.qiyoujiage.com/") 53 | header = { 54 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36' 55 | } 56 | response = request('GET', 'http://www.qiyoujiage.com/' + self._region + '.shtml', headers=header) # 定义头信息发送请求返回response对象 57 | response.encoding = 'utf-8' #不写这句会乱码 58 | soup = BeautifulSoup(response.text, "lxml") 59 | dls = soup.select("#youjia > dl") 60 | self._state = soup.select("#youjiaCont > div")[1].contents[0].strip() 61 | 62 | for dl in dls: 63 | k = re.search("\d+", dl.select('dt')[0].text).group() 64 | self._entries[k] = dl.select('dd')[0].text 65 | self._entries["update_time"] = datetime.datetime.now().strftime('%Y-%m-%d') 66 | self._entries["tips"] = soup.select("#youjiaCont > div:nth-of-type(2) > span")[0].text.strip() # 油价涨跌信息 67 | 68 | @property 69 | def name(self): 70 | return self._name 71 | 72 | @property 73 | def state(self): 74 | return self._state 75 | 76 | @property 77 | def icon(self): 78 | return ICON 79 | 80 | @property 81 | def extra_state_attributes(self): 82 | return self._entries 83 | --------------------------------------------------------------------------------