hass-core/homeassistant/components/esphome/sensor.py
Ville Skyttä 761d7f21e9 Upgrade pylint (#27279)
* Upgrade pylint to 2.4.2 and astroid to 2.3.1

https://pylint.readthedocs.io/en/latest/whatsnew/2.4.html
https://pylint.readthedocs.io/en/latest/whatsnew/changelog.html#what-s-new-in-pylint-2-4-1
https://pylint.readthedocs.io/en/latest/whatsnew/changelog.html#what-s-new-in-pylint-2-4-2

* unnecessary-comprehension fixes

* invalid-name fixes

* self-assigning-variable fixes

* Re-enable not-an-iterable

* used-before-assignment fix

* invalid-overridden-method fixes

* undefined-variable __class__ workarounds

https://github.com/PyCQA/pylint/issues/3090

* no-member false positive disabling

* Remove some no longer needed disables

* using-constant-test fix

* Disable import-outside-toplevel for now

* Disable some apparent no-value-for-parameter false positives

* invalid-overridden-method false positive disables

https://github.com/PyCQA/pylint/issues/3150

* Fix unintentional Entity.force_update override in AfterShipSensor
2019-10-07 08:17:39 -07:00

94 lines
2.5 KiB
Python

"""Support for esphome sensors."""
import logging
import math
from typing import Optional
from aioesphomeapi import SensorInfo, SensorState, TextSensorInfo, TextSensorState
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
) -> None:
"""Set up esphome sensors based on a config entry."""
await platform_async_setup_entry(
hass,
entry,
async_add_entities,
component_key="sensor",
info_type=SensorInfo,
entity_type=EsphomeSensor,
state_type=SensorState,
)
await platform_async_setup_entry(
hass,
entry,
async_add_entities,
component_key="text_sensor",
info_type=TextSensorInfo,
entity_type=EsphomeTextSensor,
state_type=TextSensorState,
)
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
# pylint: disable=invalid-overridden-method
class EsphomeSensor(EsphomeEntity):
"""A sensor implementation for esphome."""
@property
def _static_info(self) -> SensorInfo:
return super()._static_info
@property
def _state(self) -> Optional[SensorState]:
return super()._state
@property
def icon(self) -> str:
"""Return the icon."""
return self._static_info.icon
@esphome_state_property
def state(self) -> Optional[str]:
"""Return the state of the entity."""
if math.isnan(self._state.state):
return None
return "{:.{prec}f}".format(
self._state.state, prec=self._static_info.accuracy_decimals
)
@property
def unit_of_measurement(self) -> str:
"""Return the unit the value is expressed in."""
return self._static_info.unit_of_measurement
class EsphomeTextSensor(EsphomeEntity):
"""A text sensor implementation for ESPHome."""
@property
def _static_info(self) -> "TextSensorInfo":
return super()._static_info
@property
def _state(self) -> Optional["TextSensorState"]:
return super()._state
@property
def icon(self) -> str:
"""Return the icon."""
return self._static_info.icon
@esphome_state_property
def state(self) -> Optional[str]:
"""Return the state of the entity."""
return self._state.state