Add native ESPHome API text sensor (#19377)

* Update

* 🚑 Lint
pull/19436/head
Otto Winter 2018-12-17 20:51:59 +01:00 committed by GitHub
parent 4b124e4c25
commit 8861909ea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 2 deletions

View File

@ -10,7 +10,8 @@ from homeassistant.helpers.typing import HomeAssistantType
if TYPE_CHECKING:
# pylint: disable=unused-import
from aioesphomeapi import SensorInfo, SensorState # noqa
from aioesphomeapi import ( # noqa
SensorInfo, SensorState, TextSensorInfo, TextSensorState)
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
@ -20,7 +21,8 @@ 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
from aioesphomeapi import ( # noqa
SensorInfo, SensorState, TextSensorInfo, TextSensorState)
await platform_async_setup_entry(
hass, entry, async_add_entities,
@ -28,6 +30,12 @@ async def async_setup_entry(hass: HomeAssistantType,
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
)
class EsphomeSensor(EsphomeEntity):
@ -60,3 +68,27 @@ class EsphomeSensor(EsphomeEntity):
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
@property
def state(self) -> Optional[str]:
"""Return the state of the entity."""
if self._state is None:
return None
return self._state.state