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

 找回密码
 立即注册
查看: 14197|回复: 27

[插件集成] 彩云天气动态图标版 Custom UI Card for lovelace【原创修改】

[复制链接]

65

主题

853

帖子

3038

积分

论坛元老

Rank: 8Rank: 8

积分
3038
金钱
2180
HASS币
40
发表于 2019-1-23 11:47:47 | 显示全部楼层 |阅读模式
本帖最后由 plutosherry 于 2019-1-29 21:45 编辑

彩云天气 动态图标完整版【Caiyun Weather for lovelace】更新信息见2楼


此自定义card仅支持lovelace如何启用lovelace请问囧神,毕竟是他举办UI设计活动带大家入的坑啊,啊哈哈哈!!!
目前暂时先发布一个mini版本的,完整版(rest版因自定义forecast问题)还在修改中。mini版UI修改自darksky模板,不明白什么是darksky的可自行前往 Homeassistant.io 搜索了解。
惯例先上图:

彩云mini版

彩云mini版

图中左侧就是darksky的模板,右侧是依照此修改的动态天气图标模板。

一、下载压缩包文件(见文末)并按指定路径覆盖(有能力修改代码的童靴可自行编辑 caiyun-weather-card-mini.js 修改相关文件的路径)
相关文件存放位置如下:
自定义card文件:www/lovelace_ui/resources/caiyun-weather-card-mini.js
样式表文件:www/lovelace_ui/resources/caiyun-weather-card-mini.css
天气动态图标:www/lovelace_ui/images/weather_animated_icons    (目前只引用部分图标,其余图标的引用将在彩云官方V4版api后才会支持)
彩云天气插件:custom_components/weather/caiyunweather.py
主文件配置:ui-lovelace.yaml

二、由于mini版采用自定义插件获取天气数据,所以需要配置一下configuration.yaml当然需要重启生效!!!
weather:
  - platform: caiyunweather
    api_key:         [color=#ff0000]  <--------------(API请修改成你自己的,什么?不会申请?去彩云官网申请免费开发者账号即可获取免费API_token)[/color]
    latitude: 30.704655      # {{states.zone.home.attributes.latitude}}
    longitude: 111.27620636  # {{states.zone.home.attributes.longitude}}


三、部分文件内容修改位置说明
当然是需要配置一下lovelace了,如此才能生效card呀!
ui-lovelace.yaml中添加如下信息:(resource自然是必须的!!如果已经有views部分请直接添加其下的type即可!!
resources:
  - url: /local/lovelace_ui/resources/caiyun-weather-card-mini.js        [color=#ff0000]  <--------------(如需修改文件路径,请修改此处)[/color]
    type: module

title: MYHOME
views:
  - title: HEADER
    icon: 'mdi:menu'
    badges: []
    cards:
      - type: custom:caiyun-weather-card-mini
        title: '彩云天气'
        location: '湖北宜昌'
        entity_sun: sun.sun
        weather: weather.caiyunweather


这是自定义card的文件内容:
caiyun-weather-card-mini.js
class CaiyunWeatherCardChart extends HTMLElement {

  set hass(hass) {
    if (!this.content) {
      const card = document.createElement('ha-card');
      const link = document.createElement('link');
      link.type = 'text/css';
      link.rel = 'stylesheet';
      link.href = '/local/lovelace_ui/resources/caiyun-weather-card-mini.css';        [color=#ff0000]  <--------------(如需修改文件路径,请修改此处)[/color]
      card.appendChild(link);
      this.content = document.createElement('div');
      this.content.className = 'card';
      card.appendChild(this.content);
      this.appendChild(card);
    }

    const sunriseTime = function(time) {
      const date = new Date(time);
      return date.toLocaleTimeString([],
        { hour:'2-digit', minute:'2-digit' }
      );
    }

    var weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const weatherIcons = {
      'sunny': 'day',
      'clear-night': 'night',
      'partlycloudyday': 'cloudy-day-3',
      'partlycloudynight': 'cloudy-night-3',
      'cloudy': 'cloudy',
      'rainy': 'rainy-6',
      'snowy': 'snowy-6',
      'windy': 'cloudy',
      'fog': 'cloudy',
    };

    const weatherState = {
      'sunny': '晴天',
      'clear-night': '晴夜',
      'partlycloudyday': '多云',
      'partlycloudynight': '多云',
      'cloudy': '阴',
      'rainy': '雨',
      'snowy': '雪',
      'windy': '风',
      'fog': '雾'
    };

    const windDirections = [
      '北风',
      '东北偏北风',
      '东北风',
      '东北偏东风',
      '东风',
      '东南偏东风',
      '东南风',
      '东南偏南风',
      '南风',
      '西南偏南风',
      '西南风',
      '西南偏西风',
      '西风',
      '西北偏西风',
      '西北风',
      '西北偏北风',
      '北风'
    ];

    const weatherObj = hass.states[this.config.weather];
    const title = this.config.title;
    const location = this.config.location;
    const currentCondition = weatherObj.state;
    const temperature = Math.round(weatherObj.attributes.temperature);
    const humidity = Math.round(weatherObj.attributes.humidity);
    const windbearing = windDirections[Math.round((weatherObj.attributes.wind_bearing / 360) * 16)];
    const sunObj = hass.states[this.config.entity_sun];
    const forecast = weatherObj.attributes.forecast.slice(1, 5);

    this.content.innerHTML = `

    <ha-card header="${title}" class="ha-card">
      <div class="card">
            <div class="icon bigger" style="background: none, url(/local/lovelace_ui/images/weather_animated_icons/${weatherIcons[currentCondition]}.svg) no-repeat; background-size: contain;"></div>
            <div class="te">
              <li class="tempsize">${temperature}<span class="aunit2">°</span></li>
              <li class="xiii"><iron-icon icon="mdi:weather-sunset-up"></iron-icon>${sunriseTime(sunObj.attributes.next_rising)}</li>
              <li class="xiio"><iron-icon icon="mdi:weather-sunset-down"></iron-icon>${sunriseTime(sunObj.attributes.next_setting)}</li>
            </div>
            <div class="variations">
               <li><iron-icon icon="mdi:compass"></iron-icon>${location}</li>
               <br>
               <li class="xii"><iron-icon icon="mdi:weather-windy"></iron-icon>${windbearing}<li>
               <li class="xii"><iron-icon icon="mdi:water-percent"></iron-icon>${humidity}
               <span class="unit">%</span></li>
            </div>
            <div class="clear2">
              ${forecast.map(daily => `
                  <div class="day">
                      <span class="dayname">${weekday[new Date((daily.datetime).toString().split(' ')[0]).getDay()]}</span>
                      <br><i class="icon" style="background: none, url(/local/lovelace_ui/images/weather_animated_icons/${weatherIcons[daily.condition]}.svg) no-repeat; background-size: contain;"></i>
                      <br><span class="highTemp">${daily.temperature}°/${daily.templow}°</span>
                      <br><iron-icon icon="mdi:weather-rainy"></iron-icon><span class="highTemp">${daily.precipitation}</span><span class="unit">mm</span>
                  </div>`).join('')}
            </div>
      </div>
    </ha-card>

    `;
  }

  setConfig(config) {
    if (!config.weather || !config.title) {
      throw new Error('Please define entities');
    }
    this.config = config;
    this.title = config.title;

  }

  getCardSize() {
    return 1;
  }

}

customElements.define('caiyun-weather-card-mini', CaiyunWeatherCardChart);


四、到此为止是不是很容易就完成了?配完了就开心的玩耍吧
最后,本人非专业程序猿,业余爱好之余就改了这么个东东,希望各位能够喜欢,你们的支持是我最大的动力



Caiyun_mini_animated_UI_Card.rar

42.75 KB, 阅读权限: 10, 下载次数: 278

彩云动态UI

评分

参与人数 2金钱 +25 收起 理由
Hyejeong + 5 感谢楼主分享!
+ 20 感谢楼主分享!

查看全部评分

回复

使用道具 举报

65

主题

853

帖子

3038

积分

论坛元老

Rank: 8Rank: 8

积分
3038
金钱
2180
HASS币
40
 楼主| 发表于 2019-1-23 11:52:59 | 显示全部楼层
本帖最后由 plutosherry 于 2019-1-29 21:44 编辑

彩云天气 动态图标完整版【Caiyun Weather for lovelace】
github 链接:https://github.com/plutosherry/caiyun-weather-card

目前版本v1.0下来修改下api就可以用,git已设public如有兴趣后续共同开发的可git pull .... thanks!!!
回复

使用道具 举报

74

主题

1942

帖子

7885

积分

元老级技术达人

积分
7885
金钱
5893
HASS币
430

活跃会员教程狂人

发表于 2019-1-23 11:58:15 | 显示全部楼层
感谢大佬分享,这个好看多了,拿走咯。
所有过往,皆为序章。
回复

使用道具 举报

5

主题

277

帖子

2298

积分

金牌会员

Rank: 6Rank: 6

积分
2298
金钱
2011
HASS币
50
发表于 2019-1-23 12:02:50 | 显示全部楼层
温度下上限能不能改成曲线那种,感觉更直观些 微信截图_20190123120229.png
回复

使用道具 举报

65

主题

853

帖子

3038

积分

论坛元老

Rank: 8Rank: 8

积分
3038
金钱
2180
HASS币
40
 楼主| 发表于 2019-1-23 12:04:44 | 显示全部楼层
he_qr 发表于 2019-1-23 12:02
温度下上限能不能改成曲线那种,感觉更直观些

完整版会考虑
回复

使用道具 举报

8

主题

863

帖子

5124

积分

论坛元老

Rank: 8Rank: 8

积分
5124
金钱
4261
HASS币
0
发表于 2019-1-23 12:21:11 | 显示全部楼层
大神666666
回复

使用道具 举报

65

主题

853

帖子

3038

积分

论坛元老

Rank: 8Rank: 8

积分
3038
金钱
2180
HASS币
40
 楼主| 发表于 2019-1-23 12:45:44 | 显示全部楼层
咸味土豆 发表于 2019-1-23 12:14
大佬,反馈个问题,IOS客户端显示有一点小问题。日出日落和星期重叠。
...

用的什么浏览器?我的苹果上safari、chrome、homeassistant.app显示都是正常的哎
回复

使用道具 举报

74

主题

1942

帖子

7885

积分

元老级技术达人

积分
7885
金钱
5893
HASS币
430

活跃会员教程狂人

发表于 2019-1-23 13:01:27 | 显示全部楼层
plutosherry 发表于 2019-1-23 12:45
用的什么浏览器?我的苹果上safari、chrome、homeassistant.app显示都是正常的哎

搞定了,应该是缓存之类的事儿。多谢回复。
所有过往,皆为序章。
回复

使用道具 举报

7

主题

1975

帖子

5663

积分

论坛元老

流水无味

Rank: 8Rank: 8

积分
5663
金钱
3688
HASS币
145

灌水之王

发表于 2019-1-23 15:30:19 | 显示全部楼层
he_qr 发表于 2019-1-23 12:02
温度下上限能不能改成曲线那种,感觉更直观些

这个温度漂亮!!
回复

使用道具 举报

18

主题

688

帖子

3764

积分

元老级技术达人

积分
3764
金钱
3066
HASS币
60
发表于 2019-1-23 15:45:15 | 显示全部楼层
感谢分享。
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-25 13:29 , Processed in 0.538043 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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