Add support for Soma Tilt devices (#49734)
This commit is contained in:
parent
8b99adc1dc
commit
a9785f1b41
7 changed files with 213 additions and 78 deletions
|
@ -1,16 +1,28 @@
|
|||
"""Support for Soma Covers."""
|
||||
import logging
|
||||
from __future__ import annotations
|
||||
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.components.cover import ATTR_POSITION, CoverEntity
|
||||
from homeassistant.components.cover import (
|
||||
ATTR_POSITION,
|
||||
ATTR_TILT_POSITION,
|
||||
DEVICE_CLASS_BLIND,
|
||||
DEVICE_CLASS_SHADE,
|
||||
SUPPORT_CLOSE,
|
||||
SUPPORT_CLOSE_TILT,
|
||||
SUPPORT_OPEN,
|
||||
SUPPORT_OPEN_TILT,
|
||||
SUPPORT_SET_POSITION,
|
||||
SUPPORT_SET_TILT_POSITION,
|
||||
SUPPORT_STOP,
|
||||
SUPPORT_STOP_TILT,
|
||||
CoverEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import API, DEVICES, DOMAIN, SomaEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
from .utils import is_api_response_success
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
|
@ -20,56 +32,105 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up the Soma cover platform."""
|
||||
|
||||
api = hass.data[DOMAIN][API]
|
||||
devices = hass.data[DOMAIN][DEVICES]
|
||||
entities: list[SomaTilt | SomaShade] = []
|
||||
|
||||
async_add_entities(
|
||||
[SomaCover(cover, hass.data[DOMAIN][API]) for cover in devices], True
|
||||
for device in devices:
|
||||
# Assume a shade device if the type is not present in the api response (Connect <2.2.6)
|
||||
if "type" in device and device["type"].lower() == "tilt":
|
||||
entities.append(SomaTilt(device, api))
|
||||
else:
|
||||
entities.append(SomaShade(device, api))
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class SomaTilt(SomaEntity, CoverEntity):
|
||||
"""Representation of a Soma Tilt device."""
|
||||
|
||||
_attr_device_class = DEVICE_CLASS_BLIND
|
||||
_attr_supported_features = (
|
||||
SUPPORT_OPEN_TILT
|
||||
| SUPPORT_CLOSE_TILT
|
||||
| SUPPORT_STOP_TILT
|
||||
| SUPPORT_SET_TILT_POSITION
|
||||
)
|
||||
|
||||
@property
|
||||
def current_cover_tilt_position(self):
|
||||
"""Return the current cover tilt position."""
|
||||
return self.current_position
|
||||
|
||||
class SomaCover(SomaEntity, CoverEntity):
|
||||
"""Representation of a Soma cover device."""
|
||||
@property
|
||||
def is_closed(self):
|
||||
"""Return if the cover tilt is closed."""
|
||||
return self.current_position == 0
|
||||
|
||||
def close_cover(self, **kwargs):
|
||||
"""Close the cover."""
|
||||
def close_cover_tilt(self, **kwargs):
|
||||
"""Close the cover tilt."""
|
||||
response = self.api.set_shade_position(self.device["mac"], 100)
|
||||
if response["result"] != "success":
|
||||
_LOGGER.error(
|
||||
"Unable to reach device %s (%s)", self.device["name"], response["msg"]
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while closing the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
self.set_position(0)
|
||||
|
||||
def open_cover(self, **kwargs):
|
||||
"""Open the cover."""
|
||||
response = self.api.set_shade_position(self.device["mac"], 0)
|
||||
if response["result"] != "success":
|
||||
_LOGGER.error(
|
||||
"Unable to reach device %s (%s)", self.device["name"], response["msg"]
|
||||
def open_cover_tilt(self, **kwargs):
|
||||
"""Open the cover tilt."""
|
||||
response = self.api.set_shade_position(self.device["mac"], -100)
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while opening the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
self.set_position(100)
|
||||
|
||||
def stop_cover(self, **kwargs):
|
||||
"""Stop the cover."""
|
||||
# Set cover position to some value where up/down are both enabled
|
||||
self.current_position = 50
|
||||
def stop_cover_tilt(self, **kwargs):
|
||||
"""Stop the cover tilt."""
|
||||
response = self.api.stop_shade(self.device["mac"])
|
||||
if response["result"] != "success":
|
||||
_LOGGER.error(
|
||||
"Unable to reach device %s (%s)", self.device["name"], response["msg"]
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while stopping the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
# Set cover position to some value where up/down are both enabled
|
||||
self.set_position(50)
|
||||
|
||||
def set_cover_position(self, **kwargs):
|
||||
"""Move the cover shutter to a specific position."""
|
||||
self.current_position = kwargs[ATTR_POSITION]
|
||||
response = self.api.set_shade_position(
|
||||
self.device["mac"], 100 - kwargs[ATTR_POSITION]
|
||||
)
|
||||
if response["result"] != "success":
|
||||
_LOGGER.error(
|
||||
"Unable to reach device %s (%s)", self.device["name"], response["msg"]
|
||||
def set_cover_tilt_position(self, **kwargs):
|
||||
"""Move the cover tilt to a specific position."""
|
||||
# 0 -> Closed down (api: 100)
|
||||
# 50 -> Fully open (api: 0)
|
||||
# 100 -> Closed up (api: -100)
|
||||
target_api_position = 100 - ((kwargs[ATTR_TILT_POSITION] / 50) * 100)
|
||||
response = self.api.set_shade_position(self.device["mac"], target_api_position)
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while setting the cover position ({self.name}): {response["msg"]}'
|
||||
)
|
||||
self.set_position(kwargs[ATTR_TILT_POSITION])
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the entity with the latest data."""
|
||||
response = await self.get_shade_state_from_api()
|
||||
|
||||
api_position = int(response["position"])
|
||||
|
||||
if "closed_upwards" in response.keys():
|
||||
self.current_position = 50 + ((api_position * 50) / 100)
|
||||
else:
|
||||
self.current_position = 50 - ((api_position * 50) / 100)
|
||||
|
||||
|
||||
class SomaShade(SomaEntity, CoverEntity):
|
||||
"""Representation of a Soma Shade device."""
|
||||
|
||||
_attr_device_class = DEVICE_CLASS_SHADE
|
||||
_attr_supported_features = (
|
||||
SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP | SUPPORT_SET_POSITION
|
||||
)
|
||||
|
||||
@property
|
||||
def current_cover_position(self):
|
||||
"""Return the current position of cover shutter."""
|
||||
"""Return the current cover position."""
|
||||
return self.current_position
|
||||
|
||||
@property
|
||||
|
@ -77,22 +138,45 @@ class SomaCover(SomaEntity, CoverEntity):
|
|||
"""Return if the cover is closed."""
|
||||
return self.current_position == 0
|
||||
|
||||
def close_cover(self, **kwargs):
|
||||
"""Close the cover."""
|
||||
response = self.api.set_shade_position(self.device["mac"], 100)
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while closing the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
|
||||
def open_cover(self, **kwargs):
|
||||
"""Open the cover."""
|
||||
response = self.api.set_shade_position(self.device["mac"], 0)
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while opening the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
|
||||
def stop_cover(self, **kwargs):
|
||||
"""Stop the cover."""
|
||||
response = self.api.stop_shade(self.device["mac"])
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while stopping the cover ({self.name}): {response["msg"]}'
|
||||
)
|
||||
# Set cover position to some value where up/down are both enabled
|
||||
self.set_position(50)
|
||||
|
||||
def set_cover_position(self, **kwargs):
|
||||
"""Move the cover shutter to a specific position."""
|
||||
self.current_position = kwargs[ATTR_POSITION]
|
||||
response = self.api.set_shade_position(
|
||||
self.device["mac"], 100 - kwargs[ATTR_POSITION]
|
||||
)
|
||||
if not is_api_response_success(response):
|
||||
raise HomeAssistantError(
|
||||
f'Error while setting the cover position ({self.name}): {response["msg"]}'
|
||||
)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the cover with the latest data."""
|
||||
try:
|
||||
_LOGGER.debug("Soma Cover Update")
|
||||
response = await self.hass.async_add_executor_job(
|
||||
self.api.get_shade_state, self.device["mac"]
|
||||
)
|
||||
except RequestException:
|
||||
_LOGGER.error("Connection to SOMA Connect failed")
|
||||
self.is_available = False
|
||||
return
|
||||
if response["result"] != "success":
|
||||
_LOGGER.error(
|
||||
"Unable to reach device %s (%s)", self.device["name"], response["msg"]
|
||||
)
|
||||
self.is_available = False
|
||||
return
|
||||
response = await self.get_shade_state_from_api()
|
||||
|
||||
self.current_position = 100 - int(response["position"])
|
||||
self.is_available = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue