Add native ESPHome API component (#19334)
* Create esphomelib component * Update requirements * Remove python 2 string literals * MVP * Remove config flow * Remove config flow translations * Use dispatcher more * Use data classes * Type Hints * Cleanup on remove * Use helper and cleanup * Fix HA stop listener cleanup * Add config flow * Fix SyntaxError for Py3.5 * Update * Lint * Re-do tests * ✨ Rename to ESPHome * Better error message for resolve errors * Fix tests when aioesphomeapi not installed * Refactor mock * Update requirements * Add strings.json * 🍵 100% config flow test coverage
This commit is contained in:
parent
f9c02889b2
commit
a08bab7b18
11 changed files with 888 additions and 0 deletions
62
homeassistant/components/sensor/esphome.py
Normal file
62
homeassistant/components/sensor/esphome.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
"""Support for esphome sensors."""
|
||||
import logging
|
||||
import math
|
||||
from typing import Optional, TYPE_CHECKING
|
||||
|
||||
from homeassistant.components.esphome import EsphomeEntity, \
|
||||
platform_async_setup_entry
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
# pylint: disable=unused-import
|
||||
from aioesphomeapi import SensorInfo, SensorState # noqa
|
||||
|
||||
DEPENDENCIES = ['esphome']
|
||||
_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."""
|
||||
# pylint: disable=redefined-outer-name
|
||||
from aioesphomeapi import SensorInfo, SensorState # noqa
|
||||
|
||||
await platform_async_setup_entry(
|
||||
hass, entry, async_add_entities,
|
||||
component_key='sensor',
|
||||
info_type=SensorInfo, entity_type=EsphomeSensor,
|
||||
state_type=SensorState
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
|
||||
@property
|
||||
def state(self) -> Optional[str]:
|
||||
"""Return the state of the entity."""
|
||||
if self._state is None:
|
||||
return None
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue