Static typing for Uptime (#51638)

* uptime typing

* Clean up name type

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Yuval Aboulafia 2021-06-09 15:30:33 +03:00 committed by GitHub
parent 6e20edc30c
commit e78c656bfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 10 deletions

View File

@ -71,6 +71,7 @@ homeassistant.components.systemmonitor.*
homeassistant.components.tcp.* homeassistant.components.tcp.*
homeassistant.components.tts.* homeassistant.components.tts.*
homeassistant.components.upcloud.* homeassistant.components.upcloud.*
homeassistant.components.uptime.*
homeassistant.components.vacuum.* homeassistant.components.vacuum.*
homeassistant.components.water_heater.* homeassistant.components.water_heater.*
homeassistant.components.weather.* homeassistant.components.weather.*

View File

@ -1,14 +1,18 @@
"""Platform to retrieve uptime for Home Assistant.""" """Platform to retrieve uptime for Home Assistant."""
from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import (
CONF_NAME,
CONF_UNIT_OF_MEASUREMENT,
DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_TIMESTAMP,
PLATFORM_SCHEMA,
SensorEntity,
) )
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT 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
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
DEFAULT_NAME = "Uptime" DEFAULT_NAME = "Uptime"
@ -26,9 +30,14 @@ PLATFORM_SCHEMA = vol.All(
) )
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the uptime sensor platform.""" """Set up the uptime sensor platform."""
name = config.get(CONF_NAME) name = config[CONF_NAME]
async_add_entities([UptimeSensor(name)], True) async_add_entities([UptimeSensor(name)], True)
@ -36,23 +45,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class UptimeSensor(SensorEntity): class UptimeSensor(SensorEntity):
"""Representation of an uptime sensor.""" """Representation of an uptime sensor."""
def __init__(self, name): def __init__(self, name: str) -> None:
"""Initialize the uptime sensor.""" """Initialize the uptime sensor."""
self._name = name self._name = name
self._state = dt_util.now().isoformat() self._state = dt_util.now().isoformat()
@property @property
def name(self): def name(self) -> str:
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property @property
def device_class(self): def device_class(self) -> str:
"""Return device class.""" """Return device class."""
return DEVICE_CLASS_TIMESTAMP return DEVICE_CLASS_TIMESTAMP
@property @property
def state(self): def state(self) -> str:
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state

View File

@ -792,6 +792,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.uptime.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.vacuum.*] [mypy-homeassistant.components.vacuum.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true