Add state dependent icons to moon sensor (#28743)

* have sensor.moon use state dependent icons

Material design icons have icons for all sensor.states, let's use these natively in the component.
Based on the Season icons, tried to change accordingly.

* Update sensor.py

added state constants

* Update sensor.py

fixed missing mdi:

* order of constants. moon icons and use self.state

change order of constants to alphabetical, changed order in Moon_icons to alphabetical, used self.state for icon lookup

* replace the strings

 replace the strings for self.state with the constants

* removed quotes

* removed spaces, empty line

* local Black

* Sort imports and move import to file header


Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
This commit is contained in:
Marius 2019-11-19 19:10:15 +01:00 committed by Franck Nijhof
parent 5fcfdee6b5
commit 5203c72fca

View file

@ -1,19 +1,38 @@
"""Support for tracking the moon phases."""
import logging
from astral import Astral
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Moon"
ICON = "mdi:brightness-3"
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",
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
@ -31,7 +50,7 @@ class MoonSensor(Entity):
"""Representation of a Moon sensor."""
def __init__(self, name):
"""Initialize the sensor."""
"""Initialize the moon sensor."""
self._name = name
self._state = None
@ -44,29 +63,27 @@ class MoonSensor(Entity):
def state(self):
"""Return the state of the device."""
if self._state == 0:
return "new_moon"
return STATE_NEW_MOON
if self._state < 7:
return "waxing_crescent"
return STATE_WAXING_CRESCENT
if self._state == 7:
return "first_quarter"
return STATE_FIRST_QUARTER
if self._state < 14:
return "waxing_gibbous"
return STATE_WAXING_GIBBOUS
if self._state == 14:
return "full_moon"
return STATE_FULL_MOON
if self._state < 21:
return "waning_gibbous"
return STATE_WANING_GIBBOUS
if self._state == 21:
return "last_quarter"
return "waning_crescent"
return STATE_LAST_QUARTER
return STATE_WANING_CRESCENT
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
return MOON_ICONS.get(self.state, "mdi:brightness-3")
async def async_update(self):
"""Get the time and updates the states."""
from astral import Astral
today = dt_util.as_local(dt_util.utcnow()).date()
self._state = Astral().moon_phase(today)