2019-04-03 15:40:03 +00:00
|
|
|
"""Support for TCP socket based sensors."""
|
2021-05-22 14:45:18 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-24 17:51:06 +00:00
|
|
|
from typing import Final
|
2016-02-14 00:03:56 +00:00
|
|
|
|
2021-05-22 14:45:18 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
2021-05-24 10:03:43 +00:00
|
|
|
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT
|
2021-05-22 14:45:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-01-24 17:51:06 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
2021-05-22 14:45:18 +00:00
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
from .common import TCP_PLATFORM_SCHEMA, TcpEntity
|
2016-10-22 04:14:35 +00:00
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
PLATFORM_SCHEMA: Final = PARENT_PLATFORM_SCHEMA.extend(TCP_PLATFORM_SCHEMA)
|
2016-02-14 00:03:56 +00:00
|
|
|
|
|
|
|
|
2021-05-22 14:45:18 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
2022-01-24 17:51:06 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
2021-05-22 14:45:18 +00:00
|
|
|
) -> None:
|
2016-10-22 04:14:35 +00:00
|
|
|
"""Set up the TCP Sensor."""
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([TcpSensor(hass, config)])
|
2016-02-17 17:26:53 +00:00
|
|
|
|
|
|
|
|
2021-05-24 10:03:43 +00:00
|
|
|
class TcpSensor(TcpEntity, SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of a TCP socket based sensor."""
|
|
|
|
|
2016-02-17 17:26:53 +00:00
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self) -> StateType:
|
2016-02-22 13:42:11 +00:00
|
|
|
"""Return the state of the device."""
|
2016-02-17 17:26:53 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity."""
|
2016-10-22 04:14:35 +00:00
|
|
|
return self._config[CONF_UNIT_OF_MEASUREMENT]
|