Improve type hint in eddystone sensor entity (#77135)

This commit is contained in:
epenet 2022-08-22 11:36:59 +02:00 committed by GitHub
parent 361f82f5fa
commit 5cb91d7cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,7 +24,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import Event, HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -59,10 +59,10 @@ def setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Validate configuration, create devices and start monitoring thread."""
bt_device_id = config.get("bt_device_id")
bt_device_id: int = config[CONF_BT_DEVICE_ID]
beacons = config[CONF_BEACONS]
devices = []
beacons: dict[str, dict[str, str]] = config[CONF_BEACONS]
devices: list[EddystoneTemp] = []
for dev_name, properties in beacons.items():
namespace = get_from_conf(properties, CONF_NAMESPACE, 20)
@ -78,12 +78,12 @@ def setup_platform(
if devices:
mon = Monitor(hass, devices, bt_device_id)
def monitor_stop(_service_or_event):
def monitor_stop(event: Event) -> None:
"""Stop the monitor thread."""
_LOGGER.info("Stopping scanner for Eddystone beacons")
mon.stop()
def monitor_start(_service_or_event):
def monitor_start(event: Event) -> None:
"""Start the monitor thread."""
_LOGGER.info("Starting scanner for Eddystone beacons")
mon.start()
@ -96,9 +96,9 @@ def setup_platform(
_LOGGER.warning("No devices were added")
def get_from_conf(config, config_key, length):
def get_from_conf(config: dict[str, str], config_key: str, length: int) -> str | None:
"""Retrieve value from config and validate length."""
string = config.get(config_key)
string = config[config_key]
if len(string) != length:
_LOGGER.error(
"Error in configuration parameter %s: Must be exactly %d "
@ -113,44 +113,30 @@ def get_from_conf(config, config_key, length):
class EddystoneTemp(SensorEntity):
"""Representation of a temperature sensor."""
def __init__(self, name, namespace, instance):
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = TEMP_CELSIUS
_attr_should_poll = False
def __init__(self, name: str, namespace: str, instance: str) -> None:
"""Initialize a sensor."""
self._name = name
self._attr_name = name
self.namespace = namespace
self.instance = instance
self.bt_addr = None
self.temperature = STATE_UNKNOWN
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the device."""
return self.temperature
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return SensorDeviceClass.TEMPERATURE
@property
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return TEMP_CELSIUS
@property
def should_poll(self):
"""Return the polling state."""
return False
class Monitor:
"""Continuously scan for BLE advertisements."""
def __init__(self, hass, devices, bt_device_id):
def __init__(
self, hass: HomeAssistant, devices: list[EddystoneTemp], bt_device_id: int
) -> None:
"""Construct interface object."""
self.hass = hass
@ -174,7 +160,7 @@ class Monitor:
)
self.scanning = False
def start(self):
def start(self) -> None:
"""Continuously scan for BLE advertisements."""
if not self.scanning:
self.scanner.start()
@ -182,7 +168,7 @@ class Monitor:
else:
_LOGGER.debug("start() called, but scanner is already running")
def process_packet(self, namespace, instance, temperature):
def process_packet(self, namespace, instance, temperature) -> None:
"""Assign temperature to device."""
_LOGGER.debug(
"Received temperature for <%s,%s>: %d", namespace, instance, temperature
@ -197,7 +183,7 @@ class Monitor:
dev.temperature = temperature
dev.schedule_update_ha_state()
def stop(self):
def stop(self) -> None:
"""Signal runner to stop and join thread."""
if self.scanning:
_LOGGER.debug("Stopping")