Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atributtes and dates on anniversary day #5

Closed
pollinolas opened this issue Nov 6, 2019 · 2 comments · Fixed by #6
Closed

Atributtes and dates on anniversary day #5

pollinolas opened this issue Nov 6, 2019 · 2 comments · Fixed by #6

Comments

@pollinolas
Copy link

pollinolas commented Nov 6, 2019

hi,with the last upgrade i think u forgot the underscore on the new attributes,I've also seen that if it's birthday, it will mark one year less,so i edit the sensor.py changing the date format to my country too,
changes are on lines 14/15 and 70 for my format date
thanks for your work:
i tried to fix the problem with years on birthday day but i dont be able,i hope you can fix this issues in a next update,thanks again

"""Platform for sensor integration."""

import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME,
)

from homeassistant.helpers.entity import Entity
from datetime import datetime, date, timedelta

CONF_DATE = "date"
ATTR_YEARS_NEXT = "years_at_next_anniversary"
ATTR_YEARS_CURRENT = "current_years"
ATTR_DATE = "date"
CONF_ICON_NORMAL = "icon_normal"
CONF_ICON_TODAY = "icon_today"
CONF_ICON_TOMORROW = "icon_tomorrow"

DEFAULT_ICON_NORMAL = "mdi:calendar-clock"
DEFAULT_ICON_TODAY = "mdi:calendar-star"
DEFAULT_ICON_TOMORROW = "mdi:calendar"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DATE): cv.date,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_ICON_NORMAL, default=DEFAULT_ICON_NORMAL): cv.icon,
vol.Optional(CONF_ICON_TODAY, default=DEFAULT_ICON_TODAY): cv.icon,
vol.Optional(CONF_ICON_TOMORROW, default=DEFAULT_ICON_TOMORROW): cv.icon,

})

TRACKABLE_DOMAINS = ["sensor"]

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Setup the sensor platform."""
async_add_entities([anniversaries(config)],True)

class anniversaries(Entity):
def init(self, config):
"""Initialize the sensor."""
self._name = config.get(CONF_NAME)
self._date = config.get(CONF_DATE)
self._icon_normal = config.get(CONF_ICON_NORMAL)
self._icon_today = config.get(CONF_ICON_TODAY)
self._icon_tomorrow = config.get(CONF_ICON_TOMORROW)
self._icon = self._icon_normal
self._years_next = 0
self._years_current = 0
self._state = 0

@property
def name(self):
    """Return the name of the sensor."""
    return self._name

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

@property 
def device_state_attributes(self):
    """Return the state attributes."""
    res = {}
    res[ATTR_YEARS_NEXT] = self._years_next
    res[ATTR_YEARS_CURRENT] = self._years_current
    res[ATTR_DATE] = datetime.strftime(self._date,"%d-%m-%Y")
    return res

@property
def icon(self):
    return self._icon

async def async_update(self):
    today = date.today()
    nextDate = date(today.year, self._date.month, self._date.day)
    daysRemaining = 0
    years = today.year - self._date.year
    if today < nextDate:
        daysRemaining = (nextDate - today).days
    elif today == nextDate:
        daysRemaining = 0
    elif today > nextDate:
        nextDate = date(today.year + 1, self._date.month, self._date.day)
        daysRemaining = (nextDate - today).days
        years = years + 1

    if daysRemaining == 0:
        self._icon = self._icon_today
    elif daysRemaining == 1:
        self._icon = self._icon_tomorrow
    else:
        self._icon = self._icon_normal
    self._state = daysRemaining
    self._years_next = years 
    self._years_current = years - 1
pinkywafer added a commit that referenced this issue Nov 6, 2019
@pinkywafer
Copy link
Owner

Thanks for this. I did indeed miss the underscores!
I've fixed the years on anniversary day
I'm adding a date_format configuration option
next release should be available shortly!

pinkywafer added a commit that referenced this issue Nov 6, 2019
* closes #5 and adds date format option

* add date_format config option

* add date_format config option
@pollinolas
Copy link
Author

thanks a lot again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants