2019-04-03 15:40:03 +00:00
|
|
|
"""Support for tracking the moon phases."""
|
2017-02-03 07:43:03 +00:00
|
|
|
import logging
|
|
|
|
|
2019-11-19 18:10:15 +00:00
|
|
|
from astral import Astral
|
2017-02-03 07:43:03 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2017-02-03 07:43:03 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-11-19 18:10:15 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
import homeassistant.util.dt as dt_util
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Moon"
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2019-11-19 18:10:15 +00:00
|
|
|
STATE_FIRST_QUARTER = "first_quarter"
|
|
|
|
STATE_FULL_MOON = "full_moon"
|
|
|
|
STATE_LAST_QUARTER = "last_quarter"
|
|
|
|
STATE_NEW_MOON = "new_moon"
|
|
|
|
STATE_WANING_CRESCENT = "waning_crescent"
|
|
|
|
STATE_WANING_GIBBOUS = "waning_gibbous"
|
|
|
|
STATE_WAXING_GIBBOUS = "waxing_gibbous"
|
|
|
|
STATE_WAXING_CRESCENT = "waxing_crescent"
|
|
|
|
|
|
|
|
MOON_ICONS = {
|
|
|
|
STATE_FIRST_QUARTER: "mdi:moon-first-quarter",
|
|
|
|
STATE_FULL_MOON: "mdi:moon-full",
|
|
|
|
STATE_LAST_QUARTER: "mdi:moon-last-quarter",
|
|
|
|
STATE_NEW_MOON: "mdi:moon-new",
|
|
|
|
STATE_WANING_CRESCENT: "mdi:moon-waning-crescent",
|
|
|
|
STATE_WANING_GIBBOUS: "mdi:moon-waning-gibbous",
|
|
|
|
STATE_WAXING_CRESCENT: "mdi:moon-waxing-crescent",
|
|
|
|
STATE_WAXING_GIBBOUS: "mdi:moon-waxing-gibbous",
|
|
|
|
}
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-02-03 07:43:03 +00:00
|
|
|
"""Set up the Moon sensor."""
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([MoonSensor(name)], True)
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MoonSensor(Entity):
|
|
|
|
"""Representation of a Moon sensor."""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
2019-11-19 18:10:15 +00:00
|
|
|
"""Initialize the moon sensor."""
|
2017-02-03 07:43:03 +00:00
|
|
|
self._name = name
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2020-04-20 03:35:49 +00:00
|
|
|
"""Return the name of the entity."""
|
2017-02-03 07:43:03 +00:00
|
|
|
return self._name
|
|
|
|
|
2020-04-20 03:35:49 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the entity."""
|
|
|
|
return "moon__phase"
|
|
|
|
|
2017-02-03 07:43:03 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2017-03-09 21:54:04 +00:00
|
|
|
if self._state == 0:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_NEW_MOON
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state < 7:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_WAXING_CRESCENT
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state == 7:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_FIRST_QUARTER
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state < 14:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_WAXING_GIBBOUS
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state == 14:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_FULL_MOON
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state < 21:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_WANING_GIBBOUS
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._state == 21:
|
2019-11-19 18:10:15 +00:00
|
|
|
return STATE_LAST_QUARTER
|
|
|
|
return STATE_WANING_CRESCENT
|
2017-02-03 07:43:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
2019-12-03 00:19:56 +00:00
|
|
|
return MOON_ICONS.get(self.state)
|
2017-02-03 07:43:03 +00:00
|
|
|
|
2018-06-04 12:44:55 +00:00
|
|
|
async def async_update(self):
|
2017-02-03 07:43:03 +00:00
|
|
|
"""Get the time and updates the states."""
|
|
|
|
today = dt_util.as_local(dt_util.utcnow()).date()
|
|
|
|
self._state = Astral().moon_phase(today)
|