Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Lindvig authored Jan 15, 2023
1 parent bff3e7d commit 983365a
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 84 deletions.
94 changes: 62 additions & 32 deletions custom_components/flagdays_dk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import logging

from .flagdays_dk_api import flagDays_DK
from .flagdays_dk import flagdays_dk
from collections import OrderedDict

from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
)
from .const import (
DOMAIN,
CONF_CLIENT,
CONF_FLAGS,
CONF_FLAGS_DAYS,
CONF_HIDE_PAST,
CONF_TIME_OFFSET,
CONF_EXCLUDE,
CONF_FLAGDAYS,
CONF_INCLUDE,
CONF_OFFSET,
CONF_PLATFORM,
DEFAULT_FLAG,
DEFAULT_TIME_OFFSET,
DEFAULT_DATE_FORMAT,
DEFAULT_OFFSET,
KEY_DATE,
KEY_FRIENDLY_NAME,
KEY_NAME,
KEY_PRIORITY,
)

_LOGGER: logging.Logger = logging.getLogger(__package__)
Expand All @@ -31,33 +32,62 @@ async def async_setup(hass, config):
if conf is None:
return True

coordinates = {}
coordinates["lat"] = config.get(CONF_LATITUDE, hass.config.latitude)
coordinates["lon"] = config.get(CONF_LONGITUDE, hass.config.longitude)
# Create a instance of the flagdays_dk
flagdays = flagdays_dk(
include=config[DOMAIN].get(CONF_INCLUDE, []),
exclude=config[DOMAIN].get(CONF_EXCLUDE, []),
)

def flagdayFromSensor(entity):
flagdayObj = hass.states.get(customFlagday)
if KEY_DATE in flagdayObj.attributes:
return {
flagdayObj.attributes[KEY_FRIENDLY_NAME]: {
KEY_DATE: flagdayObj.attributes[KEY_DATE].strftime(
DEFAULT_DATE_FORMAT
),
KEY_PRIORITY: priorityCheck(flagdayObj.attributes),
}
}

def flagdayFromConfig(customFlagday):
flagdayData = dict(customFlagday)
flagdayData.update({KEY_PRIORITY: priorityCheck(customFlagday)})
return {customFlagday.pop(KEY_NAME): flagdayData}

def priorityCheck(payload, priority=0):
return priority if not KEY_PRIORITY in payload else payload[KEY_PRIORITY]

# Dict to hold the possible custom flagdays
customFlagdays = {}
for customFlagday in config[DOMAIN].get(CONF_FLAGDAYS):

# Sensor or Group of Sensors
if type(customFlagday) is str:
domain = customFlagday.split(".", 1)[0]

# Load flags - append default
flags = config[DOMAIN].get(CONF_FLAGS, [DEFAULT_FLAG])
if not DEFAULT_FLAG in flags:
flags.append(DEFAULT_FLAG)
_LOGGER.debug(f"Flags loaded from config: { flags }")
# Sensor
if domain == "sensor":
customFlagdays.update(flagdayFromSensor(customFlagday))

# Load time offest else DEFAULT_TIME_OFFSET
time_offset = config[DOMAIN].get(CONF_TIME_OFFSET, DEFAULT_TIME_OFFSET)
_LOGGER.debug(f"Time offset set to: { time_offset } minutes")
# Group of Sensors
elif domain == "group":
for customFlagday in hass.states.get(customFlagday).attributes[
"entity_id"
]:
if customFlagday.split(".", 1)[0] == "sensor":
customFlagdays.update(flagdayFromSensor(customFlagday))

# Load boolean to hide flagdays in the past
hidePast = config[DOMAIN].get(CONF_HIDE_PAST, True)
_LOGGER.debug(f"Hide flagdays in the past is { hidePast }")
# Element from YAML
elif type(customFlagday) is OrderedDict:
customFlagdays.update(flagdayFromConfig(customFlagday))

# Load private flagdays
privateFlagDays = config[DOMAIN].get(CONF_FLAGS_DAYS, [])
if privateFlagDays:
_LOGGER.debug(f"Added { len(privateFlagDays) } private flagdays")
# Add the custom flagdays
flagdays.add(customFlagdays)

hass.data[DOMAIN] = {
CONF_CLIENT: flagDays_DK(
flags, coordinates, time_offset, privateFlagDays, hidePast
)
CONF_CLIENT: flagdays,
CONF_OFFSET: config[DOMAIN].get(CONF_OFFSET, DEFAULT_OFFSET),
}

# Add sensors
Expand Down
27 changes: 14 additions & 13 deletions custom_components/flagdays_dk/const.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
CONF_CLIENT = "client"
CONF_FLAGS = "flags"
CONF_FLAGS_DAYS = "flagdays"
CONF_HIDE_PAST = "hide_past"
CONF_TIME_OFFSET = "time_offset"
CONF_EXCLUDE = "exclude"
CONF_FLAGDAYS = "flagdays"
CONF_INCLUDE = "include"
CONF_OFFSET = "offset"
CONF_PLATFORM = "sensor"
DEFAULT_COORDINATES = {"lat": 55.395903819648304, "lon": 10.388097722778282}
DEFAULT_FLAG = "dannebrog"
DEFAULT_TIME_OFFSET = 10
DEFAULT_DATE_FORMAT = "%d-%m-%Y"
DEFAULT_OFFSET = 10
DOMAIN = "flagdays_dk"
FLAGDAY_URL = "https://www.justitsministeriet.dk/temaer/flagning/flagdage/"
UPDATE_INTERVAL = 60

CREDITS = [
{"Created by": "J-Lindvig (https://github.com/J-Lindvig)"},
{"Data provided by": "Justitsministeriet (" + FLAGDAY_URL + ")"},
{"Sunrise/sunset provided by": "Sunrise-Sunset (https://sunrise-sunset.org/api)"},
]
CREDITS = "J-Lindvig (https://github.com/J-Lindvig)"

KEY_DATE = "date"
KEY_DATE_END = "date_end"
KEY_FLAG = "flag"
KEY_FRIENDLY_NAME = "friendly_name"
KEY_NAME = "name"
KEY_PRIORITY = "priority"
57 changes: 57 additions & 0 deletions custom_components/flagdays_dk/easter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from datetime import datetime, timedelta
import math


class easter:
def __init__(self, year=int(datetime.today().year)):

self._holidays = {}

# https://www.experimentarium.dk/faenomener/saadan-falder-paasken-hvert-aar/
a = year % 19
b = year % 4
c = year % 7
k = year // 100
p = math.floor((13 + 8 * k) / 25)
q = k / 4
M = (15 - p + k - q) % 30
N = (4 + k - q) % 7
d = (19 * a + M) % 30
e = (2 * b + 4 * c + 6 * d + N) % 7

month = 3 if 22 + d + e <= 31 else 4
day = int(22 + d + e if month == 3 else d + e - 9)
if d == 29 and e == 6:
month = 4
if a > 10:
day = 18
else:
day = 19

self._holidays["easterSunday"] = datetime.strptime(
f"{day}-{month}-{year}", "%d-%m-%Y"
)
self._holidays["goodFriday"] = self._holidays["easterSunday"] - timedelta(
days=2
)
self._holidays["christsAscension"] = self._holidays["easterSunday"] + timedelta(
days=-3, weeks=6
)
self._holidays["pentecost"] = self._holidays["easterSunday"] + timedelta(
weeks=7
)

def getGoodFriday(self):
return self._holidays["goodFriday"]

def getEasterSunday(self):
return self._holidays["easterSunday"]

def getChristsAscension(self):
return self._holidays["christsAscension"]

def getPentecost(self):
return self._holidays["pentecost"]

def getAllHolidays(self):
return self._holidays
Loading

0 comments on commit 983365a

Please sign in to comment.