1 """Platform for the Garadget cover component."""
3 from __future__
import annotations
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as COVER_PLATFORM_SCHEMA,
31 _LOGGER = logging.getLogger(__name__)
33 ATTR_AVAILABLE =
"available"
34 ATTR_SENSOR_STRENGTH =
"sensor_reflection_rate"
35 ATTR_SIGNAL_STRENGTH =
"wifi_signal_strength"
36 ATTR_TIME_IN_STATE =
"time_in_state"
38 DEFAULT_NAME =
"Garadget"
40 STATE_OFFLINE =
"offline"
41 STATE_STOPPED =
"stopped"
44 "open": CoverState.OPEN,
45 "opening": CoverState.OPENING,
46 "closed": CoverState.CLOSED,
47 "closing": CoverState.CLOSING,
48 "stopped": STATE_STOPPED,
51 COVER_SCHEMA = vol.Schema(
53 vol.Optional(CONF_ACCESS_TOKEN): cv.string,
54 vol.Optional(CONF_DEVICE): cv.string,
55 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
56 vol.Optional(CONF_PASSWORD): cv.string,
57 vol.Optional(CONF_USERNAME): cv.string,
61 PLATFORM_SCHEMA = COVER_PLATFORM_SCHEMA.extend(
62 {vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA)}
69 add_entities: AddEntitiesCallback,
70 discovery_info: DiscoveryInfoType |
None =
None,
72 """Set up the Garadget covers."""
74 devices = config[CONF_COVERS]
76 for device_id, device_config
in devices.items():
78 "name": device_config.get(CONF_NAME),
79 "device_id": device_config.get(CONF_DEVICE, device_id),
80 "username": device_config.get(CONF_USERNAME),
81 "password": device_config.get(CONF_PASSWORD),
82 "access_token": device_config.get(CONF_ACCESS_TOKEN),
91 """Representation of a Garadget cover."""
93 _attr_device_class = CoverDeviceClass.GARAGE
96 """Initialize the cover."""
117 if self.
_name_name
is None:
119 if doorconfig[
"nme"]
is not None:
120 self.
_name_name = doorconfig[
"nme"]
122 except requests.exceptions.ConnectionError
as ex:
123 _LOGGER.error(
"Unable to connect to server: %(reason)s", {
"reason": ex})
124 self.
_state_state = STATE_OFFLINE
126 self.
_name_name = DEFAULT_NAME
129 "Garadget device %(device)s seems to be offline",
132 self.
_name_name = DEFAULT_NAME
133 self.
_state_state = STATE_OFFLINE
137 """Try to remove token."""
143 """Return the name of the cover."""
144 return self.
_name_name
148 """Return True if entity is available."""
153 """Return the device state attributes."""
156 if self.
signalsignal
is not None:
157 data[ATTR_SIGNAL_STRENGTH] = self.
signalsignal
162 if self.
sensorsensor
is not None:
163 data[ATTR_SENSOR_STRENGTH] = self.
sensorsensor
172 """Return if the cover is closed."""
173 if self.
_state_state
is None:
175 return self.
_state_state == CoverState.CLOSED
178 """Get new token for usage during this session."""
180 "grant_type":
"password",
184 url = f
"{self.particle_url}/oauth/token"
185 ret = requests.post(url, auth=(
"particle",
"particle"), data=args, timeout=10)
188 return ret.json()[
"access_token"]
190 _LOGGER.error(
"Unable to retrieve access token")
193 """Remove authorization token from API."""
194 url = f
"{self.particle_url}/v1/access_tokens/{self.access_token}"
195 ret = requests.delete(url, auth=(self.
_username_username, self.
_password_password), timeout=10)
200 _LOGGER.debug(
"Starting Watcher for command: %s ", command)
207 """Check the state of the service during an operation."""
211 """Close the cover."""
212 if self.
_state_state
not in [
"close",
"closing"]:
217 """Open the cover."""
218 if self.
_state_state
not in [
"open",
"opening"]:
223 """Stop the door where it is."""
224 if self.
_state_state
not in [
"stopped"]:
229 """Get updated status from API."""
232 _LOGGER.debug(
"Current Status: %s", status[
"status"])
233 self.
_state_state = STATES_MAP.get(status[
"status"])
235 self.
signalsignal = status[
"signal"]
236 self.
sensorsensor = status[
"sensor"]
238 except requests.exceptions.ConnectionError
as ex:
239 _LOGGER.error(
"Unable to connect to server: %(reason)s", {
"reason": ex})
240 self.
_state_state = STATE_OFFLINE
243 "Garadget device %(device)s seems to be offline",
246 self.
_state_state = STATE_OFFLINE
249 self.
_state_state
not in [CoverState.CLOSING, CoverState.OPENING]
256 """Get latest status."""
257 url = f
"{self.particle_url}/v1/devices/{self.device_id}/{var}?access_token={self.access_token}"
258 ret = requests.get(url, timeout=10)
260 for pairs
in ret.json()[
"result"].split(
"|"):
261 key = pairs.split(
"=")
262 result[key[0]] = key[1]
266 """Send commands to API."""
267 params = {
"access_token": self.
access_tokenaccess_token}
269 params[
"command"] = arg
270 url = f
"{self.particle_url}/v1/devices/{self.device_id}/{func}"
271 ret = requests.post(url, data=params, timeout=10)
None close_cover(self, **Any kwargs)
dict[str, Any] extra_state_attributes(self)
def _check_state(self, now)
def _put_command(self, func, arg=None)
bool|None is_closed(self)
def _start_watcher(self, command)
None open_cover(self, **Any kwargs)
def _get_variable(self, var)
None stop_cover(self, **Any kwargs)
def __init__(self, hass, args)
None schedule_update_ha_state(self, bool force_refresh=False)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)