Move ESPHome Source Files (#20092)

* Move ESPHome source files

* Update .coveragerc

* Update CODEOWNERS
This commit is contained in:
Otto Winter authored and Aaron Bach committed 2019-01-14 09:00:48 -07:00
1 parent e505a9b7b4
commit f225570980
8 files changed
+6 -2

No files matched your search

@@ -0,0 +1,63 @@
"""Support for ESPHome binary sensors."""
import logging
from typing import TYPE_CHECKING, Optional
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.esphome import EsphomeEntity, \
platform_async_setup_entry
if TYPE_CHECKING:
# pylint: disable=unused-import
from aioesphomeapi import BinarySensorInfo, BinarySensorState # noqa
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up ESPHome binary sensors based on a config entry."""
# pylint: disable=redefined-outer-name
from aioesphomeapi import BinarySensorInfo, BinarySensorState # noqa
await platform_async_setup_entry(
hass, entry, async_add_entities,
component_key='binary_sensor',
info_type=BinarySensorInfo, entity_type=EsphomeBinarySensor,
state_type=BinarySensorState
)
class EsphomeBinarySensor(EsphomeEntity, BinarySensorDevice):
"""A binary sensor implementation for ESPHome."""
@property
def _static_info(self) -> 'BinarySensorInfo':
return super()._static_info
@property
def _state(self) -> Optional['BinarySensorState']:
return super()._state
@property
def is_on(self):
"""Return true if the binary sensor is on."""
if self._static_info.is_status_binary_sensor:
# Status binary sensors indicated connected state.
# So in their case what's usually _availability_ is now state
return self._entry_data.available
if self._state is None:
return None
return self._state.state
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._static_info.device_class
@property
def available(self):
"""Return True if entity is available."""
if self._static_info.is_status_binary_sensor:
return True
return super().available
+82
View File
@@ -0,0 +1,82 @@
"""Support for ESPHome covers."""
import logging
from typing import TYPE_CHECKING, Optional
from homeassistant.components.cover import CoverDevice, SUPPORT_CLOSE, \
SUPPORT_OPEN, SUPPORT_STOP
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 CoverInfo, CoverState # noqa
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistantType,
entry: ConfigEntry, async_add_entities) -> None:
"""Set up ESPHome covers based on a config entry."""
# pylint: disable=redefined-outer-name
from aioesphomeapi import CoverInfo, CoverState # noqa
await platform_async_setup_entry(
hass, entry, async_add_entities,
component_key='cover',
info_type=CoverInfo, entity_type=EsphomeCover,
state_type=CoverState
)
class EsphomeCover(EsphomeEntity, CoverDevice):
"""A cover implementation for ESPHome."""
@property
def _static_info(self) -> 'CoverInfo':
return super()._static_info
@property
def _state(self) -> Optional['CoverState']:
return super()._state
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
@property
def assumed_state(self) -> bool:
"""Return true if we do optimistic updates."""
return self._static_info.is_optimistic
@property
def is_closed(self) -> Optional[bool]:
"""Return if the cover is closed or not."""
if self._state is None:
return None
return bool(self._state.state)
async def async_open_cover(self, **kwargs) -> None:
"""Open the cover."""
from aioesphomeapi.client import COVER_COMMAND_OPEN
await self._client.cover_command(key=self._static_info.key,
command=COVER_COMMAND_OPEN)
async def async_close_cover(self, **kwargs) -> None:
"""Close cover."""
from aioesphomeapi.client import COVER_COMMAND_CLOSE
await self._client.cover_command(key=self._static_info.key,
command=COVER_COMMAND_CLOSE)
async def async_stop_cover(self, **kwargs):
"""Stop the cover."""
from aioesphomeapi.client import COVER_COMMAND_STOP
await self._client.cover_command(key=self._static_info.key,
command=COVER_COMMAND_STOP)
+122
View File
@@ -0,0 +1,122 @@
"""Support for ESPHome fans."""
import logging
from typing import List, Optional, TYPE_CHECKING
from homeassistant.components.esphome import EsphomeEntity, \
platform_async_setup_entry
from homeassistant.components.fan import FanEntity, SPEED_HIGH, SPEED_LOW, \
SPEED_MEDIUM, SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, SPEED_OFF
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
if TYPE_CHECKING:
# pylint: disable=unused-import
from aioesphomeapi import FanInfo, FanState # noqa
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistantType,
entry: ConfigEntry, async_add_entities) -> None:
"""Set up ESPHome fans based on a config entry."""
# pylint: disable=redefined-outer-name
from aioesphomeapi import FanInfo, FanState # noqa
await platform_async_setup_entry(
hass, entry, async_add_entities,
component_key='fan',
info_type=FanInfo, entity_type=EsphomeFan,
state_type=FanState
)
FAN_SPEED_STR_TO_INT = {
SPEED_LOW: 0,
SPEED_MEDIUM: 1,
SPEED_HIGH: 2
}
FAN_SPEED_INT_TO_STR = {v: k for k, v in FAN_SPEED_STR_TO_INT.items()}
class EsphomeFan(EsphomeEntity, FanEntity):
"""A fan implementation for ESPHome."""
@property
def _static_info(self) -> 'FanInfo':
return super()._static_info
@property
def _state(self) -> Optional['FanState']:
return super()._state
async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
if speed == SPEED_OFF:
await self.async_turn_off()
return
await self._client.fan_command(
self._static_info.key, speed=FAN_SPEED_STR_TO_INT[speed])
async def async_turn_on(self, speed: Optional[str] = None,
**kwargs) -> None:
"""Turn on the fan."""
if speed == SPEED_OFF:
await self.async_turn_off()
return
data = {'key': self._static_info.key, 'state': True}
if speed is not None:
data['speed'] = FAN_SPEED_STR_TO_INT[speed]
await self._client.fan_command(**data)
# pylint: disable=arguments-differ
async def async_turn_off(self, **kwargs) -> None:
"""Turn off the fan."""
await self._client.fan_command(key=self._static_info.key, state=False)
async def async_oscillate(self, oscillating: bool):
"""Oscillate the fan."""
await self._client.fan_command(key=self._static_info.key,
oscillating=oscillating)
@property
def is_on(self) -> Optional[bool]:
"""Return true if the entity is on."""
if self._state is None:
return None
return self._state.state
@property
def speed(self) -> Optional[str]:
"""Return the current speed."""
if self._state is None:
return None
if not self._static_info.supports_speed:
return None
return FAN_SPEED_INT_TO_STR[self._state.speed]
@property
def oscillating(self) -> None:
"""Return the oscillation state."""
if self._state is None:
return None
if not self._static_info.supports_oscillation:
return None
return self._state.oscillating
@property
def speed_list(self) -> Optional[List[str]]:
"""Get the list of available speeds."""
if not self._static_info.supports_speed:
return None
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
@property
def supported_features(self) -> int:
"""Flag supported features."""
flags = 0
if self._static_info.supports_oscillation:
flags |= SUPPORT_OSCILLATE
if self._static_info.supports_speed:
flags |= SUPPORT_SET_SPEED
return flags
+160
View File
@@ -0,0 +1,160 @@
"""Support for ESPHome lights."""
import logging
from typing import Optional, List, Tuple, TYPE_CHECKING
from homeassistant.components.esphome import EsphomeEntity, \
platform_async_setup_entry
from homeassistant.components.light import Light, SUPPORT_FLASH, \
SUPPORT_BRIGHTNESS, SUPPORT_TRANSITION, SUPPORT_COLOR, \
SUPPORT_WHITE_VALUE, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT, ATTR_HS_COLOR, \
ATTR_FLASH, ATTR_TRANSITION, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, \
ATTR_EFFECT, ATTR_WHITE_VALUE, FLASH_SHORT, FLASH_LONG
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
import homeassistant.util.color as color_util
if TYPE_CHECKING:
# pylint: disable=unused-import
from aioesphomeapi import LightInfo, LightState # noqa
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
FLASH_LENGTHS = {
FLASH_SHORT: 2,
FLASH_LONG: 10,
}
async def async_setup_entry(hass: HomeAssistantType,
entry: ConfigEntry, async_add_entities) -> None:
"""Set up ESPHome lights based on a config entry."""
# pylint: disable=redefined-outer-name
from aioesphomeapi import LightInfo, LightState # noqa
await platform_async_setup_entry(
hass, entry, async_add_entities,
component_key='light',
info_type=LightInfo, entity_type=EsphomeLight,
state_type=LightState
)
class EsphomeLight(EsphomeEntity, Light):
"""A switch implementation for ESPHome."""
@property
def _static_info(self) -> 'LightInfo':
return super()._static_info
@property
def _state(self) -> Optional['LightState']:
return super()._state
@property
def is_on(self) -> Optional[bool]:
"""Return true if the switch is on."""
if self._state is None:
return None
return self._state.state
async def async_turn_on(self, **kwargs) -> None:
"""Turn the entity on."""
data = {'key': self._static_info.key, 'state': True}
if ATTR_HS_COLOR in kwargs:
hue, sat = kwargs[ATTR_HS_COLOR]
red, green, blue = color_util.color_hsv_to_RGB(hue, sat, 100)
data['rgb'] = (red / 255, green / 255, blue / 255)
if ATTR_FLASH in kwargs:
data['flash'] = FLASH_LENGTHS[kwargs[ATTR_FLASH]]
if ATTR_TRANSITION in kwargs:
data['transition_length'] = kwargs[ATTR_TRANSITION]
if ATTR_BRIGHTNESS in kwargs:
data['brightness'] = kwargs[ATTR_BRIGHTNESS] / 255
if ATTR_COLOR_TEMP in kwargs:
data['color_temperature'] = kwargs[ATTR_COLOR_TEMP]
if ATTR_EFFECT in kwargs:
data['effect'] = kwargs[ATTR_EFFECT]
if ATTR_WHITE_VALUE in kwargs:
data['white'] = kwargs[ATTR_WHITE_VALUE] / 255
await self._client.light_command(**data)
async def async_turn_off(self, **kwargs) -> None:
"""Turn the entity off."""
data = {'key': self._static_info.key, 'state': False}
if ATTR_FLASH in kwargs:
data['flash'] = FLASH_LENGTHS[kwargs[ATTR_FLASH]]
if ATTR_TRANSITION in kwargs:
data['transition_length'] = kwargs[ATTR_TRANSITION]
await self._client.light_command(**data)
@property
def brightness(self) -> Optional[int]:
"""Return the brightness of this light between 0..255."""
if self._state is None:
return None
return round(self._state.brightness * 255)
@property
def hs_color(self) -> Optional[Tuple[float, float]]:
"""Return the hue and saturation color value [float, float]."""
if self._state is None:
return None
return color_util.color_RGB_to_hs(
self._state.red * 255,
self._state.green * 255,
self._state.blue * 255)
@property
def color_temp(self) -> Optional[float]:
"""Return the CT color value in mireds."""
if self._state is None:
return None
return self._state.color_temperature
@property
def white_value(self) -> Optional[int]:
"""Return the white value of this light between 0..255."""
if self._state is None:
return None
return round(self._state.white * 255)
@property
def effect(self) -> Optional[str]:
"""Return the current effect."""
if self._state is None:
return None
return self._state.effect
@property
def supported_features(self) -> int:
"""Flag supported features."""
flags = SUPPORT_FLASH
if self._static_info.supports_brightness:
flags |= SUPPORT_BRIGHTNESS
flags |= SUPPORT_TRANSITION
if self._static_info.supports_rgb:
flags |= SUPPORT_COLOR
if self._static_info.supports_white_value:
flags |= SUPPORT_WHITE_VALUE
if self._static_info.supports_color_temperature:
flags |= SUPPORT_COLOR_TEMP
if self._static_info.effects:
flags |= SUPPORT_EFFECT
return flags
@property
def effect_list(self) -> List[str]:
"""Return the list of supported effects."""
return self._static_info.effects
@property
def min_mireds(self) -> float:
"""Return the coldest color_temp that this light supports."""
return self._static_info.min_mireds
@property
def max_mireds(self) -> float:
"""Return the warmest color_temp that this light supports."""
return self._static_info.max_mireds
@@ -0,0 +1,94 @@
"""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 ( # noqa
SensorInfo, SensorState, TextSensorInfo, TextSensorState)
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 ( # noqa
SensorInfo, SensorState, TextSensorInfo, TextSensorState)
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
)
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
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
@@ -0,0 +1,68 @@
"""Support for ESPHome switches."""
import logging
from typing import TYPE_CHECKING, Optional
from homeassistant.components.esphome import EsphomeEntity, \
platform_async_setup_entry
from homeassistant.components.switch import SwitchDevice
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
if TYPE_CHECKING:
# pylint: disable=unused-import
from aioesphomeapi import SwitchInfo, SwitchState # noqa
DEPENDENCIES = ['esphome']
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistantType,
entry: ConfigEntry, async_add_entities) -> None:
"""Set up ESPHome switches based on a config entry."""
# pylint: disable=redefined-outer-name
from aioesphomeapi import SwitchInfo, SwitchState # noqa
await platform_async_setup_entry(
hass, entry, async_add_entities,
component_key='switch',
info_type=SwitchInfo, entity_type=EsphomeSwitch,
state_type=SwitchState
)
class EsphomeSwitch(EsphomeEntity, SwitchDevice):
"""A switch implementation for ESPHome."""
@property
def _static_info(self) -> 'SwitchInfo':
return super()._static_info
@property
def _state(self) -> Optional['SwitchState']:
return super()._state
@property
def icon(self) -> str:
"""Return the icon."""
return self._static_info.icon
@property
def assumed_state(self) -> bool:
"""Return true if we do optimistic updates."""
return self._static_info.optimistic
@property
def is_on(self):
"""Return true if the switch is on."""
if self._state is None:
return None
return self._state.state
async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
await self._client.switch_command(self._static_info.key, True)
async def async_turn_off(self, **kwargs):
"""Turn the entity off."""
await self._client.switch_command(self._static_info.key, False)