Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions xsense/async_xsense.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,9 @@ async def get_state(self, station: Station):
raise APIFailure(f'Unable to retrieve station data: {self._lastres.status}/{text}')

async def set_state(self, entity: Entity, shadow: str, topic: str, definition: Dict):
station = entity.station
t = datetime.now()
timestamp = t.strftime('%Y%m%d%H%M%S')

desired = {
"deviceSN": entity.sn,
"shadow": shadow,
"stationSN": station.sn,
"time": timestamp,
"userId": self.userid
}
desired.update(definition.get('extra', {}))

data = {"state": {"desired": desired}}
station, data = self.build_desired_state(entity, shadow, definition)

res = await self.do_thing(station, topic, data)
return await self.do_thing(station, topic, data)

async def action(self, entity: Entity, action: str):
entity_def = entities.get(entity.type)
Expand All @@ -275,4 +262,4 @@ async def action(self, entity: Entity, action: str):
topic = action_def.get('topic')
if callable(topic):
topic = topic(entity)
await self.set_state(entity, action_def['shadow'], topic, action_def)
return await self.set_state(entity, action_def['shadow'], topic, action_def)
30 changes: 28 additions & 2 deletions xsense/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .entity import Entity
from .entity_map import entities
from .exceptions import AuthFailed
from .exceptions import AuthFailed, XSenseError
from .station import Station
from .house import House

Expand Down Expand Up @@ -233,4 +233,30 @@ def _parse_get_house_state(self, house: House, data: Dict):
def has_action(self, entity: Entity, action: str):
if entity_def := entities.get(entity.type):
return any(a for a in entity_def.get('actions', []) if a.get('action') == action)
return False
return False

def _station_for_entity(self, entity: Entity) -> Station:
station = getattr(entity, 'station', None)
if station:
return station
if isinstance(entity, Station):
return entity
raise XSenseError(f'Entity type {entity.type} has no station')

def build_desired_state(self, entity: Entity, shadow: str, definition: Dict):
station = self._station_for_entity(entity)
t = datetime.now(timezone.utc)
timestamp = t.strftime('%Y%m%d%H%M%S')

desired = {
"shadow": shadow,
"stationSN": station.sn,
"time": timestamp,
"userId": self.userid
}
if not isinstance(entity, Station):
desired["deviceSN"] = entity.sn
desired.update(definition.get('extra', {}))
desired.update(definition.get('data', {}))

return station, {"state": {"desired": desired}}
3 changes: 3 additions & 0 deletions xsense/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def set_alarm_data(self, values: dict):
if v := values.get(k):
self._alarm_data[k] = v

if safe_mode := values.get('safeMode'):
self.safe_mode = safe_mode

@property
def alarm_data(self):
return self._alarm_data
21 changes: 4 additions & 17 deletions xsense/xsense.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime
from typing import Dict

import requests
Expand Down Expand Up @@ -222,22 +222,9 @@ def get_state(self, station: Station):
raise APIFailure(f'Unable to retrieve station data: {self._lastres.status_code}/{self._lastres.text}')

def set_state(self, entity: Entity, shadow: str, topic: str, definition: Dict):
station = entity.station
t = datetime.now()
timestamp = t.strftime('%Y%m%d%H%M%S')

desired = {
"deviceSN": entity.sn,
"shadow": shadow,
"stationSN": station.sn,
"time": timestamp,
"userId": self.userid
}
desired.update(definition.get('extra', {}))

data = {"state": {"desired": desired}}
station, data = self.build_desired_state(entity, shadow, definition)

res = self.do_thing(station, topic, data)
return self.do_thing(station, topic, data)

def action(self, entity: Entity, action: str):
entity_def = entities.get(entity.type)
Expand All @@ -251,4 +238,4 @@ def action(self, entity: Entity, action: str):
topic = action_def.get('topic')
if callable(topic):
topic = topic(entity)
self.set_state(entity, action_def['shadow'], topic, action_def)
return self.set_state(entity, action_def['shadow'], topic, action_def)