Skip to content

Commit e7a301f

Browse files
authored
Enable using default ports for discovery and fix TZ (#189)
* Handle default ports * Log confirmation * Fix tzlocal for 3.9 * Lint
1 parent 711f412 commit e7a301f

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

emulated_hue/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ def __init__(
2222
hass_token: str,
2323
http_port: int,
2424
https_port: int,
25+
use_default_ports: bool,
2526
) -> None:
2627
"""Create an instance of HueEmulator."""
2728
self._loop = None
28-
self._config = Config(self, data_path, http_port, https_port)
29+
self._config = Config(self, data_path, http_port, https_port, use_default_ports)
2930
# the HA client is initialized in the async_start because it needs a running loop
3031
self._hass: Optional[HomeAssistantClient] = None
3132
self._hass_url = hass_url

emulated_hue/__main__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from aiorun import run
77

8-
from emulated_hue import HueEmulator
8+
from emulated_hue import HueEmulator, const
99

1010
IS_SUPERVISOR = os.path.isfile("/data/options.json") and os.environ.get("HASSIO_TOKEN")
1111

@@ -56,13 +56,19 @@
5656
"--http-port",
5757
type=int,
5858
help="Port to run the HTTP server (for use with reverse proxy, use with care)",
59-
default=os.getenv("HTTP_PORT", 80),
59+
default=os.getenv("HTTP_PORT", const.HUE_HTTP_PORT),
6060
)
6161
parser.add_argument(
6262
"--https-port",
6363
type=int,
6464
help="Port to run the HTTPS server (for use with reverse proxy, use with care)",
65-
default=os.getenv("HTTPS_PORT", 443),
65+
default=os.getenv("HTTPS_PORT", const.HUE_HTTPS_PORT),
66+
)
67+
parser.add_argument(
68+
"--use-default-ports-for-discovery",
69+
action="store_true",
70+
help=f"Always use HTTP port {const.HUE_HTTP_PORT} and HTTPS port {const.HUE_HTTPS_PORT} for discovery "
71+
f"regardless of actual exposed ports. Useful with reverse proxy.",
6672
)
6773

6874
args = parser.parse_args()
@@ -71,10 +77,18 @@
7177
token = args.token
7278
if args.verbose or os.getenv("VERBOSE", "").strip() == "true":
7379
logger.setLevel(logging.DEBUG)
80+
use_default_ports = False
81+
if (
82+
args.use_default_ports_for_discovery
83+
or os.getenv("USE_DEFAULT_PORTS", "").strip() == "true"
84+
):
85+
use_default_ports = True
7486
# turn down logging for hass-client
7587
logging.getLogger("hass_client").setLevel(logging.INFO)
7688

77-
hue = HueEmulator(datapath, url, token, args.http_port, args.https_port)
89+
hue = HueEmulator(
90+
datapath, url, token, args.http_port, args.https_port, use_default_ports
91+
)
7892

7993
def on_shutdown(loop):
8094
"""Call on loop shutdown."""

emulated_hue/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ async def __async_get_bridge_config(self, full_details: bool = False) -> dict:
11191119
"UTC": datetime.datetime.utcnow().isoformat().split(".")[0],
11201120
"localtime": datetime.datetime.now().isoformat().split(".")[0],
11211121
"timezone": self.config.get_storage_value(
1122-
"bridge_config", "timezone", tzlocal.get_localzone().zone
1122+
"bridge_config", "timezone", tzlocal.get_localzone().key
11231123
),
11241124
"whitelist": await self.__async_whitelist_to_bridge_config(),
11251125
"zigbeechannel": self.config.get_storage_value(

emulated_hue/config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class Config:
2727
"""Hold configuration variables for the emulated hue bridge."""
2828

2929
def __init__(
30-
self, hue: HueEmulator, data_path: str, http_port: int, https_port: int
30+
self,
31+
hue: HueEmulator,
32+
data_path: str,
33+
http_port: int,
34+
https_port: int,
35+
use_default_ports: bool,
3136
):
3237
"""Initialize the instance."""
3338
self.hue = hue
@@ -48,10 +53,15 @@ def __init__(
4853
# so this is only usefull when running a reverse proxy on the same host
4954
self.http_port = http_port
5055
self.https_port = https_port
56+
self.use_default_ports = use_default_ports
5157
if http_port != 80 or https_port != 443:
5258
LOGGER.warning(
5359
"Non default http/https ports detected. Hue apps require the bridge at the default ports 80/443, use at your own risk."
5460
)
61+
if self.use_default_ports:
62+
LOGGER.warning(
63+
"Using default HTTP port for discovery with non default HTTP/S ports. Are you using a reverse proxy?"
64+
)
5565

5666
mac_addr = str(get_mac_address(ip=self.ip_addr))
5767
if not mac_addr or len(mac_addr) < 16:

emulated_hue/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@
7171

7272
HASS = "hass"
7373
HUE = "hue"
74+
75+
HUE_HTTP_PORT = 80
76+
HUE_HTTPS_PORT = 443

emulated_hue/discovery.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from zeroconf import InterfaceChoice, ServiceInfo, Zeroconf
99

10+
from emulated_hue import const
11+
1012
from .config import Config
1113
from .utils import get_ip_pton
1214

@@ -59,7 +61,6 @@ def __init__(self, config: Config, bind_multicast: bool = True):
5961
self.daemon = True
6062

6163
self.ip_addr = config.ip_addr
62-
self.listen_port = config.http_port
6364
self.upnp_bind_multicast = bind_multicast
6465

6566
# Note that the double newline at the end of
@@ -79,7 +80,9 @@ def __init__(self, config: Config, bind_multicast: bool = True):
7980
(
8081
resp_template.format(
8182
ip_addr=config.ip_addr,
82-
port_num=config.http_port,
83+
port_num=const.HUE_HTTP_PORT
84+
if config.use_default_ports
85+
else config.http_port,
8386
bridge_id=config.bridge_id,
8487
device_type="urn:schemas-upnp-org:device:basic:1",
8588
bridge_uuid=f"uuid:{config.bridge_uid}",
@@ -93,7 +96,9 @@ def __init__(self, config: Config, bind_multicast: bool = True):
9396
(
9497
resp_template.format(
9598
ip_addr=config.ip_addr,
96-
port_num=config.http_port,
99+
port_num=const.HUE_HTTP_PORT
100+
if config.use_default_ports
101+
else config.http_port,
97102
bridge_id=config.bridge_id,
98103
device_type=f"uuid:{config.bridge_uid}",
99104
bridge_uuid=f"uuid:{config.bridge_uid}",

0 commit comments

Comments
 (0)