Skip to content

Commit

Permalink
Add from_base() methods (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 18, 2022
1 parent ff6105f commit ccfa1d9
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 5 deletions.
4 changes: 2 additions & 2 deletions androidtv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def setup(

# Fire TV
if aftv.device_properties.get("manufacturer") == "Amazon":
aftv.__class__ = FireTVSync
aftv = FireTVSync.from_base(aftv)

# Android TV
else:
aftv.__class__ = AndroidTVSync
aftv = AndroidTVSync.from_base(aftv)

# Fill in commands that are specific to the device
aftv._fill_in_commands() # pylint: disable=protected-access
Expand Down
30 changes: 30 additions & 0 deletions androidtv/androidtv/androidtv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ def __init__(
# fill in commands that can vary based on the device
BaseAndroidTV._fill_in_commands(self)

@classmethod
def from_base(cls, base_tv):
"""Construct an `AndroidTVAsync` object from a `BaseTVAsync` object.
Parameters
----------
base_tv : BaseTVAsync
The object that will be converted to an `AndroidTVAsync` object
Returns
-------
atv : AndroidTVAsync
The constructed `AndroidTVAsync` object
"""
# pylint: disable=protected-access
atv = cls(
base_tv.host,
base_tv.port,
base_tv.adbkey,
base_tv.adb_server_ip,
base_tv.adb_server_port,
base_tv._state_detection_rules,
)
atv._adb = base_tv._adb
atv.device_properties = base_tv.device_properties
atv.installed_apps = base_tv.installed_apps
atv.max_volume = base_tv.max_volume
return atv

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
30 changes: 30 additions & 0 deletions androidtv/androidtv/androidtv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ def __init__(
# fill in commands that can vary based on the device
BaseAndroidTV._fill_in_commands(self)

@classmethod
def from_base(cls, base_tv):
"""Construct an `AndroidTVSync` object from a `BaseTVSync` object.
Parameters
----------
base_tv : BaseTVSync
The object that will be converted to an `AndroidTVSync` object
Returns
-------
atv : AndroidTVSync
The constructed `AndroidTVSync` object
"""
# pylint: disable=protected-access
atv = cls(
base_tv.host,
base_tv.port,
base_tv.adbkey,
base_tv.adb_server_ip,
base_tv.adb_server_port,
base_tv._state_detection_rules,
)
atv._adb = base_tv._adb
atv.device_properties = base_tv.device_properties
atv.installed_apps = base_tv.installed_apps
atv.max_volume = base_tv.max_volume
return atv

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
30 changes: 30 additions & 0 deletions androidtv/firetv/firetv_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ def __init__(
# fill in commands that can vary based on the device
BaseFireTV._fill_in_commands(self)

@classmethod
def from_base(cls, base_tv):
"""Construct a `FireTVAsync` object from a `BaseTVAsync` object.
Parameters
----------
base_tv : BaseTVAsync
The object that will be converted to a `FireTVAsync` object
Returns
-------
ftv : FireTVAsync
The constructed `FireTVAsync` object
"""
# pylint: disable=protected-access
ftv = cls(
base_tv.host,
base_tv.port,
base_tv.adbkey,
base_tv.adb_server_ip,
base_tv.adb_server_port,
base_tv._state_detection_rules,
)
ftv._adb = base_tv._adb
ftv.device_properties = base_tv.device_properties
ftv.installed_apps = base_tv.installed_apps
ftv.max_volume = base_tv.max_volume
return ftv

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
30 changes: 30 additions & 0 deletions androidtv/firetv/firetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ def __init__(
# fill in commands that can vary based on the device
BaseFireTV._fill_in_commands(self)

@classmethod
def from_base(cls, base_tv):
"""Construct a `FireTVSync` object from a `BaseTVSync` object.
Parameters
----------
base_tv : BaseTVSync
The object that will be converted to a `FireTVSync` object
Returns
-------
ftv : FireTVSync
The constructed `FireTVSync` object
"""
# pylint: disable=protected-access
ftv = cls(
base_tv.host,
base_tv.port,
base_tv.adbkey,
base_tv.adb_server_ip,
base_tv.adb_server_port,
base_tv._state_detection_rules,
)
ftv._adb = base_tv._adb
ftv.device_properties = base_tv.device_properties
ftv.installed_apps = base_tv.installed_apps
ftv.max_volume = base_tv.max_volume
return ftv

# ======================================================================= #
# #
# Home Assistant Update #
Expand Down
4 changes: 2 additions & 2 deletions androidtv/setup_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ async def setup(

# Fire TV
if aftv.device_properties.get("manufacturer") == "Amazon":
aftv.__class__ = FireTVAsync
aftv = FireTVAsync.from_base(aftv)

# Android TV
else:
aftv.__class__ = AndroidTVAsync
aftv = AndroidTVAsync.from_base(aftv)

# Fill in commands that are specific to the device
aftv._fill_in_commands() # pylint: disable=protected-access
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def test_get_device_properties(self):
self.assertDictEqual({}, device_properties)

with patchers.patch_shell(DEVICE_PROPERTIES_GOOGLE_TV)[self.PATCH_KEY]:
self.btv.__class__ = AndroidTVSync
self.btv = AndroidTVSync.from_base(self.btv)
device_properties = self.btv.get_device_properties()
self.assertEqual(self.btv.device_properties["manufacturer"], "Google")
self.assertEqual(
Expand Down

0 comments on commit ccfa1d9

Please sign in to comment.