├── 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 |
98#
34 |
35 |
36 | ## 面板调用:
37 |
38 | - 打开概览页面-右上角三个点-编辑仪表盘
39 | - 右下角添加卡片-搜索「markdown」,添加markdown卡片
40 |
41 |
42 | - 标题按照喜好输入,内容直接拷贝仓库中提供的横排或竖排的代码即可。
43 |
44 |
45 | - 保存,完成编辑,查看效果。
46 |
47 | ## 交流
48 | - QQ群:198841186
49 |
50 | - 微信群:(添加该机器人,发送“进群”会自动发送邀请链接)
51 |
52 | 
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 |
--------------------------------------------------------------------------------