ISS cleanup (#55801)

This commit is contained in:
Yuval Aboulafia 2021-10-22 23:17:05 +03:00 committed by GitHub
parent 8da3b4c79f
commit a195418dd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,12 @@
"""Support for International Space Station data sensor.""" """Support for International Space Station binary sensor."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
import pyiss import pyiss
import requests import requests
from requests.exceptions import HTTPError
import voluptuous as vol import voluptuous as vol
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
@ -13,7 +16,10 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_SHOW_ON_MAP, CONF_SHOW_ON_MAP,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import Throttle from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -34,18 +40,23 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
) )
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(
"""Set up the ISS sensor.""" hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the ISS binary sensor."""
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
try: try:
iss_data = IssData(hass.config.latitude, hass.config.longitude) iss_data = IssData(hass.config.latitude, hass.config.longitude)
iss_data.update() iss_data.update()
except requests.exceptions.HTTPError as error: except HTTPError as error:
_LOGGER.error(error) _LOGGER.error(error)
return False return
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
show_on_map = config.get(CONF_SHOW_ON_MAP) show_on_map = config.get(CONF_SHOW_ON_MAP)
@ -56,28 +67,20 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class IssBinarySensor(BinarySensorEntity): class IssBinarySensor(BinarySensorEntity):
"""Implementation of the ISS binary sensor.""" """Implementation of the ISS binary sensor."""
_attr_device_class = DEFAULT_DEVICE_CLASS
def __init__(self, iss_data, name, show): 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._name = name self._attr_name = name
self._show_on_map = show self._show_on_map = show
@property @property
def name(self): def is_on(self) -> bool:
"""Return the name of the sensor."""
return self._name
@property
def is_on(self):
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return self.iss_data.is_above if self.iss_data else False return self.iss_data.is_above if self.iss_data else False
@property
def device_class(self):
"""Return the class of this sensor."""
return DEFAULT_DEVICE_CLASS
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
@ -92,6 +95,7 @@ class IssBinarySensor(BinarySensorEntity):
else: else:
attrs["long"] = self.iss_data.position.get("longitude") attrs["long"] = self.iss_data.position.get("longitude")
attrs["lat"] = self.iss_data.position.get("latitude") attrs["lat"] = self.iss_data.position.get("latitude")
return attrs return attrs
def update(self): def update(self):
@ -120,6 +124,6 @@ class IssData:
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()
self.position = iss.current_location() self.position = iss.current_location()
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): except (HTTPError, requests.exceptions.ConnectionError):
_LOGGER.error("Unable to retrieve data") _LOGGER.error("Unable to retrieve data")
return False return False