├── 1.PNG ├── 2.PNG ├── custom_components └── oilprice │ ├── __init__.py │ ├── manifest.json │ └── sensor.py └── readme.md /1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aalavender/OilPrice/caeb0caf2a117a31ef1358380e6facf9de5efc20/1.PNG -------------------------------------------------------------------------------- /2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aalavender/OilPrice/caeb0caf2a117a31ef1358380e6facf9de5efc20/2.PNG -------------------------------------------------------------------------------- /custom_components/oilprice/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /custom_components/oilprice/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "domain": "oilprice", 3 | "name": "oilprice", 4 | "version": "0.0.1", 5 | "documentation": "https://github.com/aalavender/OilPrice/blob/master/README.md", 6 | "dependencies": [], 7 | "codeowners": ["@aalavender"], 8 | "requirements": ["requests", "beautifulsoup4"] 9 | } 10 | -------------------------------------------------------------------------------- /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 | 35 | @asyncio.coroutine 36 | def async_setup_platform(hass, config, async_add_devices, discovery_info=None): 37 | _LOGGER.info("async_setup_platform sensor oilprice") 38 | async_add_devices([OilPriceSensor(name=config[CONF_NAME], region=config[CONF_REGION])],True) 39 | 40 | 41 | class OilPriceSensor(Entity): 42 | def __init__(self, name: str, region: str): 43 | self._name = name 44 | self._region = region 45 | self._state = None 46 | self._entries = {} 47 | 48 | def update(self): 49 | _LOGGER.info("sensor oilprice update info from http://www.qiyoujiage.com/") 50 | header = { 51 | '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' 52 | } 53 | response = request('GET', 'http://www.qiyoujiage.com/' + self._region + '.shtml', headers=header) # 定义头信息发送请求返回response对象 54 | response.encoding = 'utf-8' #不写这句会乱码 55 | soup = BeautifulSoup(response.text, "lxml") 56 | dls = soup.select("#youjia > dl") 57 | self._state = soup.select("#youjiaCont > div")[1].contents[0].strip() 58 | 59 | for dl in dls: 60 | k = re.search("\d+", dl.select('dt')[0].text).group() 61 | self._entries[k] = dl.select('dd')[0].text 62 | self._entries["update_time"] = datetime.datetime.now().strftime('%Y-%m-%d') 63 | self._entries["tips"] = soup.select("#youjiaCont > div:nth-of-type(2) > span")[0].text.strip() # 油价涨跌信息 64 | 65 | @property 66 | def name(self): 67 | return self._name 68 | 69 | @property 70 | def state(self): 71 | return self._state 72 | 73 | @property 74 | def icon(self): 75 | return ICON 76 | 77 | @property 78 | def device_state_attributes(self): 79 | return self._entries 80 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 简介 2 | 抓取最新的油价信息,包括油价涨跌提醒(默认8小时更新一次数据) 3 | 4 | 数据源地址: http://www.qiyoujiage.com 5 | 6 | # 安装 7 | 1 手动安装,放入 /custom_components/ 目录 8 | 9 | 2 hacs安装 CUSTOM REPOSITORIES中填入:https://github.com/aalavender/OilPrice 10 | 11 | # 配置 12 | **Example configuration.yaml:** 13 | ```yaml 14 | sensor: 15 | - platform: oilprice 16 | name: 最新油价 17 | region: zhejiang 18 | ``` 19 | 20 | 21 | # 前台界面 22 | 原始的界面是这样的 23 | 24 | ![avatar](https://github.com/aalavender/OilPrice/blob/master/2.PNG) 25 | 26 | ~~建议采用[markdown-mod](https://github.com/thomasloven/lovelace-markdown-mod )进行展示,效果是这样的~~ 27 | 最新的Lovelace-ui已经集成了markdown控件,格式所有区别 28 | 29 | ![avatar](https://github.com/aalavender/OilPrice/blob/master/1.PNG) 30 | 31 | list-card 的lovelace-ui配置: 32 | ``` 33 | cards: 34 | - content: > 35 | [[ 36 | sensor.zui_xin_you_jie.attributes.update_time ]] 37 | 38 | ##
92# [[ sensor.zui_xin_you_jie.attributes.92# ]] 40 |               95# 41 | [[ 42 | sensor.zui_xin_you_jie.attributes.95# ]]

98# [[ 44 | sensor.zui_xin_you_jie.attributes.98# ]]    45 |           0#柴 [[ 47 | sensor.zui_xin_you_jie.attributes.0# ]]

48 | 49 | - [[ sensor.zui_xin_you_jie.state ]] 50 | 51 | - [[ sensor.zui_xin_you_jie.attributes.tips ]] 52 | title: 浙江油价 53 | type: markdown 54 | ``` 55 | 新版的配置: 56 | ```yaml 57 | cards: 58 | - type: markdown 59 | content: > 60 | {{ 61 | state_attr('sensor.zui_xin_you_jie', 'update_time')}} 62 | 63 | ##
92# {{ state_attr('sensor.zui_xin_you_jie', '92')}} 65 |              95# 66 | 67 | {{ state_attr('sensor.zui_xin_you_jie', '95')}}

98# 68 | 69 | {{ state_attr('sensor.zui_xin_you_jie', '98')}}  70 |             0#柴油 {{ 72 | state_attr('sensor.zui_xin_you_jie', '0')}}

73 | 74 | - {{ states('sensor.zui_xin_you_jie') }} 75 | 76 | - {{ state_attr('sensor.zui_xin_you_jie', 'tips')}} 77 | ``` --------------------------------------------------------------------------------