Repository navigation
Consumer: Shelly relais #4255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Consumer: Shelly relais #4255
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d099e3a
add shelly relais
ndrsnhs c3507cf
remove factor
ndrsnhs 0c45500
add shelly relais
ndrsnhs 3729974
remove factor
ndrsnhs adadc77
Merge branch 'shelly-relais' of https://github.com/ndrsnhs/core into …
ndrsnhs 5d56b29
adjust component update
ndrsnhs f6180a5
Merge branch 'openWB:master' into shelly-relais
ndrsnhs c53ad0b
handle generations and channels
ndrsnhs afb97e2
fix auth
LKuemmel 57c3682
Review
LKuemmel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from typing import Optional, Tuple | ||
|
|
||
| from control.consumer.consumer_data import ConsumerUsage | ||
| from helpermodules.auto_str import auto_str | ||
| from modules.common.consumer_setup import ConsumerSetup | ||
| from ..vendor import vendor_descriptor | ||
|
|
||
|
|
||
| @auto_str | ||
| class ShellyConfiguration: | ||
| def __init__(self, | ||
| ip_address: Optional[str] = None, | ||
| channel: int = 0, | ||
| username: Optional[str] = None, | ||
| password: Optional[str] = None) -> None: | ||
| self.ip_address = ip_address | ||
| self.channel = channel | ||
| self.username = username | ||
| self.password = password | ||
|
|
||
|
|
||
| @auto_str | ||
| class ShellyRelais(ConsumerSetup[ShellyConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Shelly Relais (Schalten)", | ||
| type: str = "shelly_relais", | ||
| id: int = 0, | ||
| configuration: Optional[ShellyConfiguration] = None, | ||
| usage: Tuple[ConsumerUsage, ...] = (ConsumerUsage.SUSPENDABLE_ONOFF,), | ||
| **kwargs) -> None: | ||
| super().__init__(name, type, id, vendor=vendor_descriptor.configuration_factory( | ||
| ).type, configuration=configuration or ShellyConfiguration(), usage=usage, **kwargs) |
70 changes: 70 additions & 0 deletions
70
packages/modules/consumers/shelly/shelly_relais/consumer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #!/usr/bin/env python3 | ||
| from modules.common import req | ||
| from typing import Optional, Dict | ||
| from requests.auth import AuthBase | ||
| from modules.common.abstract_device import DeviceDescriptor | ||
| from modules.common.component_state import ConsumerState | ||
| from modules.common.component_type import ComponentType | ||
| from modules.common.configurable_consumer import ConfigurableConsumer | ||
| from modules.common.simcount._simcounter import SimCounterConsumer | ||
| from modules.consumers.shelly.shelly_relais.config import ShellyRelais | ||
| from modules.devices.shelly.shelly.status_handler import get_auth, get_generation, request_status | ||
|
|
||
|
|
||
| def create_consumer(config: ShellyRelais): | ||
| sim_counter: Optional[SimCounterConsumer] = None | ||
| model: Optional[str] = None | ||
| generation: int = 1 | ||
| auth: Optional[AuthBase] = None | ||
|
|
||
| def initializer(): | ||
| nonlocal sim_counter, generation, model, auth | ||
| sim_counter = SimCounterConsumer(config.id, ComponentType.CONSUMER) | ||
| generation, model = get_generation(config.configuration.ip_address) | ||
| auth = get_auth(generation, config.configuration.username, config.configuration.password) | ||
|
|
||
| def error_handler() -> None: | ||
| initializer() | ||
|
|
||
| def switch_on() -> None: | ||
| if generation == 1: | ||
| url = f"http://{config.configuration.ip_address}/relay/{config.configuration.channel}?turn=on" | ||
| else: | ||
| chan = config.configuration.channel | ||
| # gen 2 will das als on cmd /rpc/Switch.Set?id=100&on=true | ||
| url = f"http://{config.configuration.ip_address}/rpc/Switch.Set?id={chan}&on=true" | ||
| req.get_http_session().get(url, auth=auth, timeout=3) | ||
|
|
||
| def switch_off() -> None: | ||
| if generation == 1: | ||
| url = f"http://{config.configuration.ip_address}/relay/{config.configuration.channel}?turn=off" | ||
| else: | ||
| chan = config.configuration.channel | ||
| # gen 2 will das als on cmd /rpc/Switch.Set?id=100&on=true | ||
| url = f"http://{config.configuration.ip_address}/rpc/Switch.Set?id={chan}&on=false" | ||
| req.get_http_session().get(url, auth=auth, timeout=3) | ||
|
|
||
| def update() -> ConsumerState: | ||
| status = request_status(config.configuration.ip_address, generation, auth=auth) | ||
| state = parse_state(status) | ||
| return ConsumerState( | ||
| state=state | ||
| ) | ||
|
|
||
| def parse_state(status: Dict) -> bool: | ||
| try: | ||
| if generation == 1: | ||
| return status["relays"][config.configuration.channel]["ison"] | ||
| return status[f"switch:{config.configuration.channel}"]["output"] | ||
| except (KeyError, TypeError, IndexError): | ||
| raise Exception("Schaltstatus des Relais nicht lesbar.") | ||
|
|
||
| return ConfigurableConsumer(consumer_config=config, | ||
| initializer=initializer, | ||
| error_handler=error_handler, | ||
| switch_on=switch_on, | ||
| switch_off=switch_off, | ||
| update=update,) | ||
|
|
||
|
|
||
| device_descriptor = DeviceDescriptor(configuration_factory=ShellyRelais) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import logging | ||
| from typing import Dict | ||
| from typing import Tuple, List, Optional | ||
| from requests.auth import AuthBase, HTTPBasicAuth, HTTPDigestAuth | ||
|
|
||
| from modules.common import req | ||
|
|
||
|
|
@@ -23,12 +24,20 @@ def get_generation(address: str) -> Tuple[int, str]: | |
| return generation, model | ||
|
|
||
|
|
||
| def request_status(address: str, generation: Optional[int]) -> Dict: | ||
| def get_auth(generation: int, username: Optional[str], password: Optional[str]) -> Optional[AuthBase]: | ||
| if not password: | ||
| return None | ||
| if generation == 1: | ||
| return HTTPBasicAuth(username, password) if username else None | ||
| return HTTPDigestAuth("admin", password) | ||
|
|
||
|
|
||
| def request_status(address: str, generation: Optional[int], auth: Optional[AuthBase] = None) -> Dict: | ||
| if generation == 1: | ||
| status_url = "http://" + address + "/status" | ||
| else: | ||
| status_url = "http://" + address + "/rpc/Shelly.GetStatus" | ||
| return req.get_http_session().get(status_url, timeout=3).json() | ||
| return req.get_http_session().get(status_url, auth=auth, timeout=3).json() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obwohl die Session-Hooks zuerst angelegt werden, registriert Requests beim Vorbereiten der Anfrage die Auth-Hooks vor den Session-Hooks. |
||
|
|
||
|
|
||
| def parse_temperatures(status: Dict, generation: int) -> Optional[List[float]]: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beim Endpunkt /shelly braucht man bei beiden Generationen kein Auth.