Skip to content

Commit

Permalink
Add name property and cleanup properties (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecode committed Jan 20, 2023
1 parent 49ac6d6 commit c75362c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
29 changes: 15 additions & 14 deletions aioshelly/block_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(
self.blocks: list | None = None
self.coap_s: dict[str, Any] | None = None
self._settings: dict[str, Any] | None = None
self.shelly: dict[str, Any] | None = None
self._shelly: dict[str, Any] | None = None
self._status: dict[str, Any] | None = None
self._unsub_coap: Callable | None = coap_context.subscribe_updates(
options.ip_address, self._coap_message_received
Expand Down Expand Up @@ -235,7 +235,7 @@ async def update_settings(self) -> None:

async def update_shelly(self) -> None:
"""Device update for /shelly (HTTP)."""
self.shelly = await get_info(self.aiohttp_session, self.options.ip_address)
self._shelly = await get_info(self.aiohttp_session, self.options.ip_address)

async def _coap_request(self, path: str) -> asyncio.Event:
"""Device CoAP request."""
Expand Down Expand Up @@ -325,8 +325,6 @@ async def set_thermostat_state(self, channel: int = 0, **kwargs: Any) -> None:
@property
def requires_auth(self) -> bool:
"""Device check for authentication."""
assert self.shelly

if "auth" not in self.shelly:
raise WrongShellyGen

Expand Down Expand Up @@ -354,6 +352,14 @@ def status(self) -> dict[str, Any]:

return self._status

@property
def shelly(self) -> dict[str, Any]:
"""Device firmware version."""
if self._shelly is None:
raise NotInitialized

return self._shelly

@property
def gen(self) -> int:
"""Device generation: GEN1 - CoAP."""
Expand All @@ -362,28 +368,23 @@ def gen(self) -> int:
@property
def firmware_version(self) -> str:
"""Device firmware version."""
if not self.initialized:
raise NotInitialized

assert self.shelly

return cast(str, self.shelly["fw"])

@property
def model(self) -> str:
"""Device model."""
if not self.initialized:
raise NotInitialized

assert self.shelly

return cast(str, self.shelly["type"])

@property
def hostname(self) -> str:
"""Device hostname."""
return cast(str, self.settings["device"]["hostname"])

@property
def name(self) -> str:
"""Device name."""
return cast(str, self.settings["name"] or self.hostname)

@property
def last_error(self) -> ShellyError | None:
"""Return the last error during async device init."""
Expand Down
39 changes: 15 additions & 24 deletions aioshelly/rpc_device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
"""Device init."""
self.aiohttp_session: ClientSession = aiohttp_session
self.options: ConnectionOptions = options
self.shelly: dict[str, Any] | None = None
self._shelly: dict[str, Any] | None = None
self._status: dict[str, Any] | None = None
self._event: dict[str, Any] | None = None
self._config: dict[str, Any] | None = None
Expand Down Expand Up @@ -133,7 +133,7 @@ async def initialize(self, async_init: bool = False) -> None:
self.initialized = False
ip = self.options.ip_address
try:
self.shelly = await get_info(self.aiohttp_session, self.options.ip_address)
self._shelly = await get_info(self.aiohttp_session, self.options.ip_address)

if self.requires_auth:
if self.options.username is None or self.options.password is None:
Expand Down Expand Up @@ -259,8 +259,6 @@ async def ble_getconfig(self) -> ShellyBLEConfig:
@property
def requires_auth(self) -> bool:
"""Device check for authentication."""
assert self.shelly

if "auth_en" not in self.shelly:
raise WrongShellyGen

Expand Down Expand Up @@ -310,6 +308,14 @@ def config(self) -> dict[str, Any]:

return self._config

@property
def shelly(self) -> dict[str, Any]:
"""Device firmware version."""
if self._shelly is None:
raise NotInitialized

return self._shelly

@property
def gen(self) -> int:
"""Device generation: GEN2 - RPC."""
Expand All @@ -318,43 +324,28 @@ def gen(self) -> int:
@property
def firmware_version(self) -> str:
"""Device firmware version."""
assert self.shelly

if not self.initialized:
raise NotInitialized

return cast(str, self.shelly["fw_id"])

@property
def version(self) -> str:
"""Device version."""
assert self.shelly

if not self.initialized:
raise NotInitialized

return cast(str, self.shelly["ver"])

@property
def model(self) -> str:
"""Device model."""
assert self.shelly

if not self.initialized:
raise NotInitialized

return cast(str, self.shelly["model"])

@property
def hostname(self) -> str:
"""Device hostname."""
assert self.shelly

if not self.initialized:
raise NotInitialized

return cast(str, self.shelly["id"])

@property
def name(self) -> str:
"""Device name."""
return cast(str, self.config["sys"]["device"].get("name") or self.hostname)

@property
def connected(self) -> bool:
"""Return true if device is connected."""
Expand Down
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def print_device(device: BlockDevice | RpcDevice) -> None:
return

model_name = MODEL_NAMES.get(device.model) or f"Unknown ({device.model})"
print(f"** {model_name} - {device.hostname} @ {device.ip_address} **")
print(f"** {device.name} - {model_name} @ {device.ip_address} **")
print()

if device.gen == 1:
Expand Down

0 comments on commit c75362c

Please sign in to comment.