『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
楼主: ilikehassbian

[插件集成] 【老鸟回坑】无需API申请中国气象台分钟级别lovelace插件分享

  [复制链接]

22

主题

612

帖子

2757

积分

金牌会员

Rank: 6Rank: 6

积分
2757
金钱
2145
HASS币
0
发表于 2022-4-9 00:10:14 | 显示全部楼层
提示出错

Logger: homeassistant.components.weather
Source: custom_components/weathernmc/weather.py:63
Integration: Weather (documentation, issues)
First occurred: 24:07:59 (2 occurrences)
Last logged: 24:07:59

Error adding entities for domain weather with platform weathernmc
Error while setting up weathernmc platform for weather
回复

使用道具 举报

0

主题

14

帖子

68

积分

注册会员

Rank: 2

积分
68
金钱
54
HASS币
0
发表于 2022-4-10 03:22:42 | 显示全部楼层

支持大佬
回复

使用道具 举报

22

主题

612

帖子

2757

积分

金牌会员

Rank: 6Rank: 6

积分
2757
金钱
2145
HASS币
0
发表于 2022-4-11 08:23:58 | 显示全部楼层
有谁配置成功了吗?配置好仍然提示找不到实体
回复

使用道具 举报

2

主题

43

帖子

350

积分

中级会员

Rank: 3Rank: 3

积分
350
金钱
307
HASS币
0
发表于 2022-4-13 10:19:07 | 显示全部楼层
上周可用,这周就提示找不到不实体了
回复

使用道具 举报

24

主题

884

帖子

5055

积分

论坛元老

Rank: 8Rank: 8

积分
5055
金钱
4166
HASS币
20
发表于 2022-4-15 22:21:59 来自手机 | 显示全部楼层
同上,没有实体
回复

使用道具 举报

22

主题

612

帖子

2757

积分

金牌会员

Rank: 6Rank: 6

积分
2757
金钱
2145
HASS币
0
发表于 2022-4-16 14:16:24 | 显示全部楼层

已经有坛友解决了,亲测正常使用
https://bbs.hassbian.com/thread-16099-1-1.html
回复

使用道具 举报

7

主题

201

帖子

1095

积分

论坛积极会员

积分
1095
金钱
894
HASS币
0
发表于 2022-5-29 14:11:18 | 显示全部楼层
虽然还没配置成功,感谢楼主!最近被彩云API耗尽困扰中
回复

使用道具 举报

27

主题

230

帖子

1775

积分

金牌会员

Rank: 6Rank: 6

积分
1775
金钱
1545
HASS币
0
发表于 2023-5-1 13:10:48 | 显示全部楼层
关注一波
回复

使用道具 举报

0

主题

13

帖子

238

积分

中级会员

Rank: 3Rank: 3

积分
238
金钱
225
HASS币
0
发表于 2023-5-10 13:39:53 | 显示全部楼层
weather:
  - platform: weathernmc
    api_key: 123

请问这个配置是添加在哪里呢
回复

使用道具 举报

9

主题

78

帖子

940

积分

论坛积极会员

积分
940
金钱
862
HASS币
0
发表于 2023-5-12 20:06:11 | 显示全部楼层
借鉴大佬的代码,简单修改了下,
from datetime import datetime, timedelta
import logging
import time


这样后面就不用修改代码直接在配置文件配置

weather:
  - platform: weathernmc
    code: 53698


from homeassistant.components.weather import (
    WeatherEntity, ATTR_FORECAST_CONDITION,
    ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME, ATTR_FORECAST_WIND_BEARING, ATTR_FORECAST_WIND_SPEED)

from homeassistant.const import (
    TEMP_CELSIUS,
    TEMP_FAHRENHEIT,
    CONF_CODE,
    CONF_NAME,
    CONF_SCAN_INTERVAL
)

import requests
import json

_LOGGER = logging.getLogger(__name__)

VERSION = '1.0.0'
DOMAIN = 'weathernmc'

CONDITION_MAP = {
    '晴': 'sunny',
    '多云': 'cloudy',
    '局部多云': 'partlycloudy',
    '阴': 'cloudy',
    '雾': 'fog',
    '中雾': 'fog',
    '大雾': 'fog',
    '小雨': 'rainy',
    '中雨': 'rainy',
    '大雨': 'pouring',
    '暴雨': 'pouring',
    '雾': 'fog',
    '小雪': 'snowy',
    '中雪': 'snowy',
    '大雪': 'snowy',
    '暴雪': 'snowy',
    '扬沙': 'fog',
    '沙尘': 'fog',
    '雷阵雨': 'lightning-rainy',
    '冰雹': 'hail',
    '雨夹雪': 'snowy-rainy',
    '大风': 'windy',
    '薄雾': 'fog',
    '雨': 'rainy',
    '雪': 'snowy',
    '9999': 'exceptional',
    
}

def setup_platform(hass, config, add_entities, discovery_info=None):
    add_entities(
        [
            NMCWeather(
                code=config.get(CONF_CODE),
                name=config.get(CONF_NAME, 'weathernmc'),
                interval=config.get(CONF_SCAN_INTERVAL, 60)
            )
        ]
    )


class NMCWeather(WeatherEntity):

    def __init__(self, code: str, name: str, interval: int):
        self.code = code
        self._name = name
        self.interval = interval
        self.update_ts = int(time.time())
        self.update()

    @property
    def name(self):
        return self._name

    @property
    def state(self):
        skycon = self.weather_data['real']['weather']['info']
        return CONDITION_MAP[skycon]

    @property
    def temperature(self):
        return self.weather_data['real']['weather']['temperature']

    @property
    def temperature_unit(self):
        return TEMP_CELSIUS

    @property
    def humidity(self):
        return float(self.weather_data['real']['weather']['humidity']) 

    @property
    def wind_speed(self):
        return self.weather_data['real']['wind']['speed']

    @property
    def wind_bearing(self):
        return self.weather_data['real']['wind']['direct']

    @property
    def wind_speed(self):
        return self.weather_data['real']['wind']['power']


    @property
    def pressure(self):
        return round(float(self.weather_data['real']['weather']['airpressure']) / 100, 2)


    @property
    def attribution(self):
        return 'Powered by WWW.NMC.COM'

    @property
    def aqi(self):
        return self.weather_data['air']['aqi']

    @property
    def aqi_description(self):
        return self.weather_data['air']['aqi']
        
    @property
    def alert(self):
        return self.weather_data['real']['warn']['alert']

    @property
    def state_attributes(self):
        data = super(NMCWeather, self).state_attributes
        data['aqi'] = self.aqi 
        return data  

    @property
    def forecast(self):
        forecast_data = []
        for i in range(1, 7):
            time_str = self.weather_data['predict']['detail'][i]['date']
            data_dict = {
                ATTR_FORECAST_TIME: datetime.strptime(time_str, '%Y-%m-%d'),
                ATTR_FORECAST_CONDITION: CONDITION_MAP[self.weather_data['predict']['detail'][i]['day']['weather']['info']],
                ATTR_FORECAST_TEMP: self.weather_data['tempchart'][i+7]['max_temp'],
                ATTR_FORECAST_TEMP_LOW: self.weather_data['tempchart'][i+7]['min_temp'],
                ATTR_FORECAST_WIND_BEARING: self.weather_data['predict']['detail'][i]['day']['wind']['direct'],
                ATTR_FORECAST_WIND_SPEED: self.weather_data['predict']['detail'][i]['day']['wind']['power']
            }
            forecast_data.append(data_dict)

        return forecast_data

    def update(self):
        now_ts = int(time.time())
        # print(self.interval)
        if now_ts < self.update_ts + int(self.interval):
            _LOGGER.warning('WeatherNMC waring: update too fast;')
            # time.sleep(self.interval)
            # time.sleep(10)
        weather_uri = 'http://www.nmc.cn/rest/weather?stationid=%s' % self.code
        try:
            self._weather_data = requests.get(weather_uri, timeout=3).json()
            self.weather_data = self._weather_data['data']
            
            # _LOGGER.warning(self.weather_data)
            self.update_ts = int(time.time())
        except Exception as e:
            # print(weather_uri)
            # print(self.weather_data)
            _LOGGER.warning('WeatherNMC waring: %s' % e)
            time.sleep(10)
            self.update()
            
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-27 01:25 , Processed in 0.066187 second(s), 30 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表