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

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

[插件集成] 2018年【修复bug】【节假日】【纪念日】【倒计时】【农历】

  [复制链接]

0

主题

19

帖子

207

积分

论坛积极会员

积分
207
金钱
188
HASS币
0
发表于 2020-1-3 17:43:07 | 显示全部楼层
修复工作日和节假日的判断,需要的自己替换下sensor.py

#! usr/bin/python
#coding=utf-8
"""
中国节假日
版本:0.0.7
"""
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
import datetime
from datetime import timedelta
import requests
import time
import logging
from homeassistant.util import Throttle
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
     CONF_NAME)
from homeassistant.helpers.entity import generate_entity_id
from . import lunar

_Log=logging.getLogger(__name__)



DEFAULT_NAME = 'chinese_holiday'
CONF_UPDATE_INTERVAL = 'update_interval'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
    vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(minutes=360)): (vol.All(cv.time_period, cv.positive_timedelta)),
})
FESTIVAL_TYPE= {
    'new_years_day' : '元旦',
    'spring_festival' : '春节',
    'tomb_sweeping_day' : '清明节',
    'labour_day' : '国际劳动节',
    'dragon_boat_festival' : '端午节',
    'national_day' : '国庆节',
    'mid_autumn_festival' : '中秋节',
    'aniversary_forum' : 'Hassbian论坛成立一周年',
    'day_off' : '调休日需上班'
}
HOLIDAY = {
    datetime.date(year=2018, month=1, day=1): FESTIVAL_TYPE['new_years_day'],
    datetime.date(year=2018, month=2, day=11): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=2, day=15): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=16): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=17): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=18): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=19): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=20): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=21): FESTIVAL_TYPE['spring_festival'],
    datetime.date(year=2018, month=2, day=24): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=4, day=5): FESTIVAL_TYPE['tomb_sweeping_day'],
    datetime.date(year=2018, month=4, day=6): FESTIVAL_TYPE['tomb_sweeping_day'],
    datetime.date(year=2018, month=4, day=7): FESTIVAL_TYPE['tomb_sweeping_day'],
    datetime.date(year=2018, month=4, day=8): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=4, day=28): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=4, day=29): FESTIVAL_TYPE['labour_day'],
    datetime.date(year=2018, month=4, day=30): FESTIVAL_TYPE['labour_day'],
    datetime.date(year=2018, month=5, day=1): FESTIVAL_TYPE['labour_day'],
    datetime.date(year=2018, month=6, day=16): FESTIVAL_TYPE['dragon_boat_festival'],
    datetime.date(year=2018, month=6, day=17): FESTIVAL_TYPE['dragon_boat_festival'],
    datetime.date(year=2018, month=6, day=18): FESTIVAL_TYPE['dragon_boat_festival'],
    datetime.date(year=2018, month=9, day=22): FESTIVAL_TYPE['mid_autumn_festival'],
    datetime.date(year=2018, month=9, day=23): FESTIVAL_TYPE['mid_autumn_festival'],
    datetime.date(year=2018, month=9, day=24): FESTIVAL_TYPE['mid_autumn_festival'],
    datetime.date(year=2018, month=9, day=29): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=9, day=30): FESTIVAL_TYPE['day_off'],
    datetime.date(year=2018, month=10, day=1): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=2): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=3): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=4): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=5): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=6): FESTIVAL_TYPE['national_day'],
    datetime.date(year=2018, month=10, day=7): FESTIVAL_TYPE['national_day'],
}
ANNIVERSARY = {
    datetime.date(year=1993, month=12, day=15):'龙姣生日',
}
CALCULATEAGE= {
    datetime.datetime(year=1990, month=2, day=3, hour=12, minute=58, second=58): '我出生',
}

def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the movie sensor."""
    name = config[CONF_NAME]
    interval = config.get(CONF_UPDATE_INTERVAL)

    sensors = [ChineseHolidaySensor(hass, name, interval)]
    add_devices(sensors, True)

class ChineseHolidaySensor(Entity):
    def __init__(self, hass, name, interval):
        """Initialize the sensor."""
        self.client_name = name
        self._state = None
        self._hass = hass
        self.attributes = {}
        self.entity_id = generate_entity_id(
            'sensor.{}', self.client_name, hass=self._hass)
        self.update = Throttle(interval)(self._update)

    def nearest_holiday(self):
        '''查找离今天最近的法定节假日,并显示天数'''
        now_day = datetime.date.today()
        count_dict = {}
        for key in HOLIDAY.keys():
            if (key - now_day).days > 0:
                count_dict[key] = (key - now_day).days
        nearest_holiday_dict = {}
        if count_dict == {}:
            nearest_holiday_dict['name'] ='本年度已无法定节假日'
            nearest_holiday_dict['date'] = '本年度已无法定节假日'
            nearest_holiday_dict['day'] = '-1'

        else:
            nearest_holiday_dict['name'] = HOLIDAY[min(count_dict)]
            nearest_holiday_dict['date'] = min(count_dict).isoformat()
            nearest_holiday_dict['day'] = str((min(count_dict)-now_day).days)+'天'

        return nearest_holiday_dict

    def nearest_anniversary(self):
        '''查找离今天最近的纪念日,并显示天数'''
        now_day = datetime.date.today()
        count_dict = {}
        for key in ANNIVERSARY.keys():
            if (key - now_day).days > 0:
                count_dict[key] = (key - now_day).days
        nearest_anniversary_dict = {}
        if count_dict == {}:
            nearest_anniversary_dict['name'] = '未定义或无纪念日'
            nearest_anniversary_dict['date'] = '未定义或无纪念日'
            nearest_anniversary_dict['day'] = '-1'
        else:
            nearest_anniversary_dict['name'] = ANNIVERSARY[min(count_dict)]
            nearest_anniversary_dict['date'] = min(count_dict).isoformat()
            nearest_anniversary_dict['day'] = str((min(count_dict)-now_day).days)+'天'

        return nearest_anniversary_dict


    def is_holiday(self,day):
        """
        判断是否节假日, api 来自百度 apistore: [url]http://apistore.baidu.com/apiworks/servicedetail/1116.html[/url]
        :param day: 日期, 格式为 '20160404'
        :return: bool
        api = 'http://tool.bitefu.net/jiari/'
        params = {'d': day, 'apiserviceid': 1116}
        rep = requests.get(api, params)
        if rep.status_code != 200:
            return '无法获取节日数据'
        res = rep.text
        return "法定节日" if res != "0" else "非法定节日"
        """
        holiday_api = 'http://tool.bitefu.net/jiari/?d={0}'.format(day)
        rep = requests.get(holiday_api)
        if rep.status_code != 200:
            return '工作'
        holiday_date = rep.json()
        get_day = holiday_date
        result = ''
        if get_day == 0:
            result = '工作'
        elif get_day == 1:
            result = '休息'
        elif get_day == 2:
            result = '工作'
        elif get_day == 3:
            result = '休息'
        else:
            result = '工作'
        return result

    def is_holiday_today(self):
        """
        判断今天是否时节假日
        :return: bool
        """
        today = datetime.date.today().strftime('%Y%m%d')
        return self.is_holiday(today)

    @property
    def name(self):
        """Return the name of the sensor."""
        return '节假日'

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def icon(self):
        """Icon to use in the frontend, if any."""
        return 'mdi:calendar-today'



    @property
    def device_state_attributes(self):
        """Return the state attributes."""

        return self.attributes


    def _update(self):

        self._state = self.is_holiday_today()
        self.attributes['今天日期'] = datetime.date.today().strftime('%Y{y}%m{m}%d{d}').format(y='年', m='月', d='日')
        self.attributes['农历'] = lunar.getCalendar_today()['lunar']
        if 'festival' in lunar.getCalendar_today().keys():
            self.attributes['节日'] = lunar.getCalendar_today()['festival']
        self.attributes['离今天最近的法定节日'] = self.nearest_holiday()['name']
        self.attributes['法定节日日期'] = self.nearest_holiday()['date']
        self.attributes['还有'] = self.nearest_holiday()['day']
        self.attributes['最近的纪念日'] = self.nearest_anniversary()['name']
        self.attributes['纪念日日期'] = self.nearest_anniversary()['date']
        self.attributes['相隔'] = self.nearest_anniversary()['day']
        if CALCULATEAGE:
            now_day = datetime.datetime.now()
            count_dict = {}
            for key, value in CALCULATEAGE.items():
                if (now_day - key).total_seconds() > 0:
                    total_seconds = int((now_day - key).total_seconds())
                    year, remainder = divmod(total_seconds,60*60*24*365)
                    day, remainder = divmod(remainder,60*60*24)
                    hour, remainder = divmod(remainder,60*60)
                    minute, second = divmod(remainder,60)
                    self.attributes['离'+value+'过去'] = '{}年 {} 天 {} 小时 {} 分钟 {} 秒'.format(year,day,hour,minute,second)
                if (now_day - key).total_seconds() < 0:
                    total_seconds = int((key - now_day ).total_seconds())
                    year, remainder = divmod(total_seconds,60*60*24*365)
                    day, remainder = divmod(remainder,60*60*24)
                    hour, remainder = divmod(remainder,60*60)
                    minute, second = divmod(remainder,60)
                    self.attributes['离'+value+'还差']  = '{}年 {} 天 {} 小时 {} 分钟 {} 秒'.format(year,day,hour,minute,second)




        '''计算给定日期与今天日期相差的天数
        if CALCULATEAGE:
            now_day = datetime.date.today()
            count_dict = {}
            for key in CALCULATEAGE.keys():
                if (now_day - key).days > 0:
                    self.attributes[key] = str((now_day - key).days)+'天'
                if (now_day - key).days < 0:
                    self.attributes[key]  = (key - now_day).days'''
回复

使用道具 举报

4

主题

341

帖子

3422

积分

论坛元老

Rank: 8Rank: 8

积分
3422
金钱
3081
HASS币
0
发表于 2020-1-4 10:34:15 | 显示全部楼层
感谢分享!!!!
回复

使用道具 举报

22

主题

473

帖子

6293

积分

元老级技术达人

略懂一些皮毛而已!

积分
6293
金钱
5810
HASS币
40
发表于 2020-1-6 13:59:22 | 显示全部楼层
支持下 支持下
回复

使用道具 举报

0

主题

46

帖子

344

积分

中级会员

Rank: 3Rank: 3

积分
344
金钱
298
HASS币
0
发表于 2020-1-6 16:57:39 | 显示全部楼层
支持一下。
回复

使用道具 举报

9

主题

534

帖子

4530

积分

元老级技术达人

积分
4530
金钱
3986
HASS币
60
发表于 2020-1-6 17:41:16 | 显示全部楼层
感谢分享
回复

使用道具 举报

17

主题

357

帖子

2084

积分

论坛DIY达人

积分
2084
金钱
1717
HASS币
40
发表于 2020-1-6 21:28:45 | 显示全部楼层
感谢分享!
回复

使用道具 举报

19

主题

417

帖子

2465

积分

金牌会员

Rank: 6Rank: 6

积分
2465
金钱
2043
HASS币
20
发表于 2020-1-6 21:42:17 | 显示全部楼层
来看看好不好用  
回复

使用道具 举报

2

主题

109

帖子

763

积分

高级会员

Rank: 4

积分
763
金钱
654
HASS币
0
发表于 2020-1-7 23:28:36 | 显示全部楼层
纪念日怎么弄
回复

使用道具 举报

10

主题

320

帖子

1643

积分

金牌会员

Rank: 6Rank: 6

积分
1643
金钱
1323
HASS币
0
发表于 2020-1-14 20:23:42 | 显示全部楼层
erlking 发表于 2020-1-3 17:43
修复工作日和节假日的判断,需要的自己替换下sensor.py


感谢分享!!!!
回复

使用道具 举报

2

主题

121

帖子

879

积分

高级会员

Rank: 4

积分
879
金钱
758
HASS币
0
发表于 2020-1-15 14:41:12 | 显示全部楼层
膜拜大神!
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-23 13:42 , Processed in 0.093178 second(s), 30 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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