Add location to attributes and option to show position on the map (sensor.iss) (#5465)

[sensor.iss] Add location to attributes and option to show position on the map
This commit is contained in:
Fabian Affolter 2017-01-24 10:02:17 +01:00 committed by GitHub
parent 38d9dc996b
commit cd260d89cb
3 changed files with 50 additions and 46 deletions

View file

@ -133,6 +133,7 @@ omit =
homeassistant/components/binary_sensor/concord232.py homeassistant/components/binary_sensor/concord232.py
homeassistant/components/binary_sensor/flic.py homeassistant/components/binary_sensor/flic.py
homeassistant/components/binary_sensor/hikvision.py homeassistant/components/binary_sensor/hikvision.py
homeassistant/components/binary_sensor/iss.py
homeassistant/components/binary_sensor/rest.py homeassistant/components/binary_sensor/rest.py
homeassistant/components/browser.py homeassistant/components/browser.py
homeassistant/components/camera/amcrest.py homeassistant/components/camera/amcrest.py
@ -303,7 +304,6 @@ omit =
homeassistant/components/sensor/hddtemp.py homeassistant/components/sensor/hddtemp.py
homeassistant/components/sensor/hp_ilo.py homeassistant/components/sensor/hp_ilo.py
homeassistant/components/sensor/hydroquebec.py homeassistant/components/sensor/hydroquebec.py
homeassistant/components/sensor/iss.py
homeassistant/components/sensor/imap.py homeassistant/components/sensor/imap.py
homeassistant/components/sensor/imap_email_content.py homeassistant/components/sensor/imap_email_content.py
homeassistant/components/sensor/influxdb.py homeassistant/components/sensor/influxdb.py

View file

@ -5,34 +5,39 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.iss/ https://home-assistant.io/components/sensor.iss/
""" """
import logging import logging
from datetime import timedelta, datetime from datetime import timedelta
import requests import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.util import Throttle
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME)
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, ATTR_LONGITUDE, ATTR_LATITUDE)
from homeassistant.util import Throttle
REQUIREMENTS = ['pyiss==1.0.1'] REQUIREMENTS = ['pyiss==1.0.1']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_ISS_VISIBLE = 'visible'
ATTR_ISS_NEXT_RISE = 'next_rise' ATTR_ISS_NEXT_RISE = 'next_rise'
ATTR_ISS_NUMBER_PEOPLE_SPACE = 'number_of_people_in_space' ATTR_ISS_NUMBER_PEOPLE_SPACE = 'number_of_people_in_space'
CONF_SHOW_ON_MAP = 'show_on_map'
DEFAULT_NAME = 'ISS' DEFAULT_NAME = 'ISS'
DEFAULT_SENSOR_CLASS = 'visible'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_SHOW_ON_MAP, default=False): cv.boolean,
}) })
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the ISS sensor.""" """Set up the ISS sensor."""
# Validate the configuration
if None in (hass.config.latitude, hass.config.longitude): if None in (hass.config.latitude, hass.config.longitude):
_LOGGER.error("Latitude or longitude not set in Home Assistant config") _LOGGER.error("Latitude or longitude not set in Home Assistant config")
return False return False
@ -45,75 +50,74 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
show_on_map = config.get(CONF_SHOW_ON_MAP)
sensors = [] add_devices([IssBinarySensor(iss_data, name, show_on_map)], True)
sensors.append(IssSensor(iss_data, name))
add_devices(sensors, True)
class IssSensor(Entity): class IssBinarySensor(BinarySensorDevice):
"""Implementation of a ISS sensor.""" """Implementation of the ISS binary sensor."""
def __init__(self, iss_data, name): def __init__(self, iss_data, name, show):
"""Initialize the sensor.""" """Initialize the sensor."""
self.iss_data = iss_data self.iss_data = iss_data
self._state = None self._state = None
self._attributes = {} self._name = name
self._client_name = name self._show_on_map = show
self._name = ATTR_ISS_VISIBLE self.update()
self._unit_of_measurement = None
self._icon = 'mdi:eye'
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return '{} {}'.format(self._client_name, self._name) return self._name
@property @property
def state(self): def is_on(self):
"""Return the state of the sensor.""" """Return true if the binary sensor is on."""
return self._state return self.iss_data.is_above if self.iss_data else False
@property
def sensor_class(self):
"""Return the class of this sensor."""
return DEFAULT_SENSOR_CLASS
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return self._attributes if self.iss_data:
attrs = {
@property ATTR_ISS_NUMBER_PEOPLE_SPACE:
def unit_of_measurement(self): self.iss_data.number_of_people_in_space,
"""Return the unit of measurement of this entity, if any.""" ATTR_ISS_NEXT_RISE: self.iss_data.next_rise,
return self._unit_of_measurement }
if self._show_on_map:
@property attrs[ATTR_LONGITUDE] = self.iss_data.position.get('longitude')
def icon(self): attrs[ATTR_LATITUDE] = self.iss_data.position.get('latitude')
"""Icon to use in the frontend, if any.""" else:
return self._icon attrs['long'] = self.iss_data.position.get('longitude')
attrs['lat'] = self.iss_data.position.get('latitude')
return attrs
def update(self): def update(self):
"""Get the latest data from ISS API and updates the states.""" """Get the latest data from ISS API and updates the states."""
self._state = self.iss_data.is_above self.iss_data.update()
self._attributes[ATTR_ISS_NUMBER_PEOPLE_SPACE] = \
self.iss_data.number_of_people_in_space
delta = self.iss_data.next_rise - datetime.utcnow()
self._attributes[ATTR_ISS_NEXT_RISE] = int(delta.total_seconds() / 60)
class IssData(object): class IssData(object):
"""Get data from the ISS.""" """Get data from the ISS API."""
def __init__(self, latitude, longitude): def __init__(self, latitude, longitude):
"""Initialize the data object.""" """Initialize the data object."""
self.is_above = None self.is_above = None
self.next_rise = None self.next_rise = None
self.number_of_people_in_space = None self.number_of_people_in_space = None
self.position = None
self.latitude = latitude self.latitude = latitude
self.longitude = longitude self.longitude = longitude
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self):
"""Get the latest data from the ISS.""" """Get the latest data from the ISS API."""
import pyiss import pyiss
try: try:
@ -121,7 +125,7 @@ class IssData(object):
self.is_above = iss.is_ISS_above(self.latitude, self.longitude) self.is_above = iss.is_ISS_above(self.latitude, self.longitude)
self.next_rise = iss.next_rise(self.latitude, self.longitude) self.next_rise = iss.next_rise(self.latitude, self.longitude)
self.number_of_people_in_space = iss.number_of_people_in_space() self.number_of_people_in_space = iss.number_of_people_in_space()
_LOGGER.error(self.next_rise.tzinfo) self.position = iss.current_location()
except requests.exceptions.HTTPError as error: except requests.exceptions.HTTPError as error:
_LOGGER.error(error) _LOGGER.error(error)
return False return False

View file

@ -443,7 +443,7 @@ pyhomematic==0.1.20
# homeassistant.components.device_tracker.icloud # homeassistant.components.device_tracker.icloud
pyicloud==0.9.1 pyicloud==0.9.1
# homeassistant.components.sensor.iss # homeassistant.components.binary_sensor.iss
pyiss==1.0.1 pyiss==1.0.1
# homeassistant.components.sensor.lastfm # homeassistant.components.sensor.lastfm