Move smartthings base entity to separate module (#126176)

This commit is contained in:
epenet 2024-09-18 11:05:50 +02:00 committed by GitHub
parent 6325a332bd
commit fdf460b82b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 59 additions and 54 deletions

View File

@ -11,7 +11,6 @@ import logging
from aiohttp.client_exceptions import ClientConnectionError, ClientResponseError
from pysmartapp.event import EVENT_TYPE_DEVICE
from pysmartthings import Attribute, Capability, SmartThings
from pysmartthings.device import DeviceEntity
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_SECRET
@ -19,12 +18,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_loaded_integration
@ -433,42 +427,3 @@ class DeviceBroker:
updated_devices.add(device.device_id)
async_dispatcher_send(self._hass, SIGNAL_SMARTTHINGS_UPDATE, updated_devices)
class SmartThingsEntity(Entity):
"""Defines a SmartThings entity."""
_attr_should_poll = False
def __init__(self, device: DeviceEntity) -> None:
"""Initialize the instance."""
self._device = device
self._dispatcher_remove = None
self._attr_name = device.label
self._attr_unique_id = device.device_id
self._attr_device_info = DeviceInfo(
configuration_url="https://account.smartthings.com",
identifiers={(DOMAIN, device.device_id)},
manufacturer=device.status.ocf_manufacturer_name,
model=device.status.ocf_model_number,
name=device.label,
hw_version=device.status.ocf_hardware_version,
sw_version=device.status.ocf_firmware_version,
)
async def async_added_to_hass(self):
"""Device added to hass."""
async def async_update_state(devices):
"""Update device state."""
if self._device.device_id in devices:
await self.async_update_ha_state(True)
self._dispatcher_remove = async_dispatcher_connect(
self.hass, SIGNAL_SMARTTHINGS_UPDATE, async_update_state
)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect the device when removed."""
if self._dispatcher_remove:
self._dispatcher_remove()

View File

@ -15,8 +15,8 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
CAPABILITY_TO_ATTRIB = {
Capability.acceleration_sensor: Attribute.acceleration,

View File

@ -28,8 +28,8 @@ from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
ATTR_OPERATION_STATE = "operation_state"
MODE_TO_STATE = {

View File

@ -23,8 +23,8 @@ from homeassistant.const import ATTR_BATTERY_LEVEL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
VALUE_TO_STATE = {
"closed": STATE_CLOSED,

View File

@ -0,0 +1,50 @@
"""Support for SmartThings Cloud."""
from __future__ import annotations
from pysmartthings.device import DeviceEntity
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from .const import DOMAIN, SIGNAL_SMARTTHINGS_UPDATE
class SmartThingsEntity(Entity):
"""Defines a SmartThings entity."""
_attr_should_poll = False
def __init__(self, device: DeviceEntity) -> None:
"""Initialize the instance."""
self._device = device
self._dispatcher_remove = None
self._attr_name = device.label
self._attr_unique_id = device.device_id
self._attr_device_info = DeviceInfo(
configuration_url="https://account.smartthings.com",
identifiers={(DOMAIN, device.device_id)},
manufacturer=device.status.ocf_manufacturer_name,
model=device.status.ocf_model_number,
name=device.label,
hw_version=device.status.ocf_hardware_version,
sw_version=device.status.ocf_firmware_version,
)
async def async_added_to_hass(self):
"""Device added to hass."""
async def async_update_state(devices):
"""Update device state."""
if self._device.device_id in devices:
await self.async_update_ha_state(True)
self._dispatcher_remove = async_dispatcher_connect(
self.hass, SIGNAL_SMARTTHINGS_UPDATE, async_update_state
)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect the device when removed."""
if self._dispatcher_remove:
self._dispatcher_remove()

View File

@ -18,8 +18,8 @@ from homeassistant.util.percentage import (
)
from homeassistant.util.scaling import int_states_in_range
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
SPEED_RANGE = (1, 3) # off is not included

View File

@ -23,8 +23,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
async def async_setup_entry(

View File

@ -12,8 +12,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
ST_STATE_LOCKED = "locked"
ST_LOCK_ATTR_MAP = {

View File

@ -31,8 +31,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
class Map(NamedTuple):

View File

@ -12,8 +12,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN
from .entity import SmartThingsEntity
async def async_setup_entry(